环境:python
已安装Anaconda3 (64-bit) 4.4.0
(Python 3.6.1
)。其中,代码调试在Spyder 3.1.4
中进行,安装包则直接打开Anaconda Prompt
调用cmd.exe后进行。git
系统为Windows 7 和 Windows 10。github
首先,安装包时,直接用 pip install wordcloud
是会报错的,会有文件缺失的问题。这一点很多人都有相似反馈。express
解决方案:canvas
在github下载wordcloud的包,解压缩后,在对应目录下用
python setup.py install
安装。markdown
安装wordcloud时出现报错信息:网络
error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio.com/visual-cpp-build-toolsapp
这种状况下,其实直接跟着提示内容进行就好。less
解决方案:dom
打开报错提示中的连接,下载并安装
Visual C++ 2015 Build Tools
。
这个问题,我在一台电脑上这样解决(成功安装了wordcloud包并正常调试了相关Python代码)以后,尝试在另外一台电脑上再安装该包,结果出现了新问题:Visual C++ 2015 Build Tools
,从官网下载下来的安装程序visualcppbuildtools_full.exe
,打开后在安装过程当中须要联网获取文件,可是因为网络限制致使没法获取,因而没法安装!而且没有搜到含有完整安装文件的包。(注:该电脑所处的网络环境不同,通常网页中的文件下载没问题,可是build tools安装时联网获取文件就受限制而不成功。)
离线安装VC++14.0 build tools的解决方案:
- 在能正常经过
visualcppbuildtools_full.exe
联网获取安装文件的电脑上,在系统自带的cmd.exe
中在对应目录下执行:
visualcppbuildtools_full.exe /layout
以后,选择下载目录存放离线安装包,而后就能够把安装文件下载下来了。
- 而后在不能联网安装的电脑上,安装离线包便可。
此处是参考msdn上找到的方法。
当遇到安装Visual C++ 2015 Build Tools
没法获取安装文件时(visualcppbuildtools_full.exe
只有3M,却提示默认安装的话须要4G空间,可想而知须要下载很多安装文件,实际上最后下载的安装文件也有2G左右),曾经尝试过是否能够不安装4G这么大的build tools而只安装VC++14的运行库,毕竟应该只是缺库吧?
毕竟,实际上运行库Microsoft Visual C++ 2015 Redistributable Update 3
(下载连接在此)的安装程序只有十几M大小,而且可以正常下载安装。
但我试下来的结果是,安装了这个东西之后依然会有前面同样的报错。因此仍是继续老老实实下载了完整的build tools。若是你有更好的方案(不用安装这个占4G左右的你们伙,不用借助另外一个网络环境下的电脑),能够和我留言。
从github下载到的原wordcloud安装包中,提到若是安装仍是有问题的的话能够试试安装连接中的.whl
文件。
不过,我是没下载这个.whl
文件的,前面讲到的坑补完就行了。
生成云词图的过程当中,本身设置字体为“微软雅黑 粗体”,在系统(Win10)路径中找到了这个字体的路径“C:\Windows\Fonts\msyhbd.ttc”。
没想到,代码复制到第二台电脑(Win7)下执行时却报错。原本觉得都是Windows默认库里的字体应该没什么问题,打开一看才发现Win7这里的字体后缀是“ttf”!
改过就正常了。
提醒之后注意:
哪怕是Windows上执行过没问题的代码,换个一样Windows的电脑,也要注意 系统默认路径和相关文件 是否有变化!
并非全默认就都同样啊啊啊啊!!!
NotImplementedError: Gray-scale images TODO
一开始,我用的底图会出现如上报错。
若是把出错的以下这行
plt.imshow(my_wc.recolor(color_func=i_colors))
去掉,那么就不会报错,可是生成的云词图会只有形状、没有原图的颜色。
出现报错时用的底图以下:
这个图片有什么问题呢?为何会有“Gray-scale images”这种提示,难道我这个图的灰度有问题?明明好几个颜色啊好吗?甚至我专心弄了个纯黑白的图,发现纯黑白色底图都能正常执行代码。
不过,把上面出错的图,和两个不出错的图对比一下读取后的数据,就能看出问题在哪里了。
以下图所示:
coloring0=np.array(Image.open("出错图.png")) #问题图,实际上,下面的白底“底图1”就是将该图直接截图保存生成的 coloring1=np.array(Image.open("后面附上不一样背景测试图对比的底图1.bmp")) #白底 coloring2=np.array(Image.open("后面附上不一样背景测试图对比的底图2.png")) #透明底
核查报错位置源代码:
class ImageColorGenerator(object): ......#此处省略不少代码 def __call__(self, word, font_size, font_path, position, orientation, **kwargs): ......#此处省略不少代码 if patch.ndim == 2: raise NotImplementedError("Gray-scale images TODO") ......#此处省略不少代码
这就能够肯定这个报错只因 “维度为2” 引发的。
哦,终于明白这个“Gray-scale”是什么鬼了。这货,每一个点都只有2个值来代表颜色啊。后面白底的图,没有透明色,每一个点也是正常的3个值;透明底的图,多了第4个值来表示透明度。而这个“没有Gray-scale的图”,应该就是没有灰度参数吧?以前选图时候还真没注意这点,毕竟我也不是学美术出身,乍一眼也看不出来这个图的颜色没有灰度因此有可能没有灰度参数啊。
解决方案:
对于新手小白,若是不想在读取图像后直接改Value加一列的话,最简单的操做就是换个能用的图再执行吧。
若是肯定就是只要这个图的样子,那么能够转化格式,或者再不行截个图保存再用也是能够的。
总之,这个报错的缘由,不是代码问题,仅仅是图的格式问题。
这里我用plt.savefig("这里是生成图片的路径")
来存储生成的云词图。
一开始,将plt.savefig
放在plt.show
以后,结果只是生成了空白的图。后来在这篇文章《【Python】解决使用plt.savefig保存图片时一片空白》中了解到:
“在plt.show() 后实际上已经建立了一个新的空白的图片(坐标轴),这时候你再plt.savefig() 就会保存这个新生成的空白图片。”
因此,只要把plt.savefig
放在plt.show
以前便可解决这个问题。
我生成云词图的最后几行代码以下:
plt.imshow(my_wc)
plt.axis("off") plt.savefig("H:/temp/temp.jpg",dpi=200) #用反斜杠的话会报错 plt.show()
在WordCloud
中,自带.to_file
能够将云词图输出到文件。
具体方法:将上述代码中plt.savefig
一行替换为
my_wc.to_file("H:/temp/temp.jpg")
便可。
能够发现两种输出方式的区别:
-plt.savefig
默认尺寸是和终端中显示差很少的缩略版的图(大小432×288),能够经过dpi调节精度改善清晰度,具体可见本文中“图片大小和精度的影响”的描述。
-.to_file
,则输出的是每一个字都精确显示的完整云词图,很是清晰,放大后能够看到连最小的字都是清晰完整地显示,固然默认尺寸也很大。
为方便对比,可见本文后面“WordCloud参数的调节”这部分里scale=2
(使用plt.savefig
输出,dpi=200
)、scale=32
(使用plt.savefig
输出,dpi=200
)、scale=2
(使用.to_file
输出)的3个云词图。
由于此次用plt.savefig
默认生成的图感受不是很清晰,尺寸不够大,因此这里在plt.savefig
中加上了参数dpi
调整精度。
固然,一样一张图,精度越高,天然尺寸也是越大的。
不过,至于精度调整成多少合适(只为了肉眼看起来清晰),是看状况而定。虽然理论上精度越大就越清晰,可是在遮罩图、词数量等因素肯定的状况下,有时候更大的精度只是把图的尺寸放大,但肉眼可见的清晰程度并不会真的就提升。
若是图过小太密集,那么多是默认精度的局限致使不清晰。须要调整精度参数dpi
放大图片。
可是若是图片足够大,字看起来也不小,可是仍然不清晰,或者布局不天然,那么有多是云词图生成时自己的参数设置问题。可见下面的描述。
import matplotlib.pyplot as plt from wordcloud import WordCloud,ImageColorGenerator import numpy as np import PIL.Image as Image coloring=np.array(Image.open("H:\temp\meerca_2.png")) my_wc=WordCloud(background_color="white",max_words=2000, mask=coloring, max_font_size=60,random_state=42,scale=8, font_path="C:\Windows\Fonts\msyhbd.ttf").generate(word_space_split) #这里word_space_split是前面生成的以空格分割的须要生成云词的词库字符串(str)
这里简要讲下几个会影响图像清晰问题的WordCloud
的参数:
mask
:遮罩图,字的大小布局和颜色都会依据遮罩图生成。其实理论上这对字大小和清晰程度的影响不大,可是遮罩图色和背景色background_color
若是易混淆,则多是一个致使看起来不清晰的因素;另外遮罩图自身各个颜色之间的对比不强烈,也可能使图看起来层次感不够。
background_color
又是白色)因而看起来不够“清晰”。background_color
:背景色,默认黑。这个原本其实也不怎么影响清晰度,可是,就像以前在mask
中提到的,若是遮罩图像颜色过浅、背景设置白色,可能致使字看起来“不清晰”。而实际上,我对一个浅色遮罩图分别用白、黑两种背景色后发现,黑色背景的强烈对比之下会有若干很浅也很小的词浮现出来,而以前因背景色、字色过于相近而几乎没法用肉眼看出这些词。
mode
:默认“RGB”。根听说明文档,若是想设置透明底色的云词图,那么能够设置background_color=None, mode="RGBA"
background_color
设置为"white"
或"black"
时,生成的云词确实是对应的“白色”“黑色”;可是按照上述参数设置透明色时,结果出来依然是白色。background_color
设置为"white"
或"black"
,仍是None
加上mode="RGBA"
,结果都是把背景部分当作黑色图块,自动匹配黑色的字!——也就是并无实现透明底的云词。底图1(白底,640×435):
底图2(透明底,300×300):
底图1与底图2生成云词图效果对比,除background_color
和mode
参数之外其它参数不变:
注:1. 两个底图尺寸也有点区别。生成的图片是用plt.savefig
(参数dpi=200
),也能够看出生成图在布局上有差别。2. 本文中,其它云词图实例中,均是使用“底图1”(自己是白色底)这个图。
max_font_size
:最大字号。源文件中也有讲到,图的生成会依据最大字号等因素去自动判断词的布局。经测试,哪怕同一个图像,只要图自己尺寸不同(好比我把一个300×300的图拉大到600×600再去当遮罩),那么一样的字号也是会有不一样的效果。原理想一想也很天然,字号决定了字的尺寸,而图的尺寸变了之后,最大字相对于图的尺寸比例天然就变了。因此,须要根据指望显示的效果,去调整最大字号参数值。 min_font_size
:最小字号。不设置的状况下,默认是4。尝试了设置比4大的字号,例如八、10,结果就是本来小于设定值且大于4号的词都直接不显示了,其它内容和未设置该值时都同样。relative_scaling
:表示词频和云词图中字大小的关系参数,默认0.5。为0时,表示只考虑词排序,而不考虑词频数;为1时,表示两倍词频的词也会用两倍字号显示。本文中的案例,均用的默认值,未特别设置该参数。scale
:根听说明文档,当云词图很大的,加大该值会比使用更大的图更快,但值越高也会越慢(计算更复杂)。默认值是1。实际测试中,更大的值,确实输出图像看起来更精细(较小较浅的词会颜色更重,也感受清楚,大的词差别不明显)。不过,可能因为我选的图不大、词也没有不少,因此差距并无很大,缩小排列一下就基本上辨别不出多少差异了。 len(word_space_split)=6310
)和图没有很大的状况下,词不变,图不变,则scale越大,运行速度越慢。实际上,本案例中取“32”时已经比“2”慢了不少秒,这个时间差能够体会体会到,本文暂不对时间差和效率问题进行精确研究了。plt.savefig
输出,dpi=200
,实际输出尺寸为1200×800。)
是否是没看出什么区别?那么下面放下“2”“32”对应的云词图自己。
scale=2
的云词图以下(使用plt.savefig
输出,dpi=200
):
scale=32
的云词图以下(使用plt.savefig
输出,dpi=200
):
scale=2
的云词图以下(使用.to_file
输出):
注:能够注意上面3个图中,右上方“惟有”字样的右上角“如同”字样(较小字)的显示差别,进行对比。
random_state
:不一样的值会让字图的分布不同。*注:本文的云词图案例中,词库里“1f60a”之类的字符没有去掉,这些原本是表明某些emoji的。我我的以为这不算乱码吧,因此留着了。
*附上源文件wordcloud.py
中对WordCloud
这一函数的各个参数的解释:
Word cloud object for generating and drawing. Parameters ---------- font_path : string Font path to the font that will be used (OTF or TTF). Defaults to DroidSansMono path on a Linux machine. If you are on another OS or don't have this font, you need to adjust this path. width : int (default=400) Width of the canvas. height : int (default=200) Height of the canvas. prefer_horizontal : float (default=0.90) The ratio of times to try horizontal fitting as opposed to vertical. If prefer_horizontal < 1, the algorithm will try rotating the word if it doesn't fit. (There is currently no built-in way to get only vertical words.) mask : nd-array or None (default=None) If not None, gives a binary mask on where to draw words. If mask is not None, width and height will be ignored and the shape of mask will be used instead. All white (#FF or #FFFFFF) entries will be considerd "masked out" while other entries will be free to draw on. [This changed in the most recent version!] scale : float (default=1) Scaling between computation and drawing. For large word-cloud images, using scale instead of larger canvas size is significantly faster, but might lead to a coarser fit for the words. min_font_size : int (default=4) Smallest font size to use. Will stop when there is no more room in this size. font_step : int (default=1) Step size for the font. font_step > 1 might speed up computation but give a worse fit. max_words : number (default=200) The maximum number of words. stopwords : set of strings or None The words that will be eliminated. If None, the build-in STOPWORDS list will be used. background_color : color value (default="black") Background color for the word cloud image. max_font_size : int or None (default=None) Maximum font size for the largest word. If None, height of the image is used. mode : string (default="RGB") Transparent background will be generated when mode is "RGBA" and background_color is None. relative_scaling : float (default=.5) Importance of relative word frequencies for font-size. With relative_scaling=0, only word-ranks are considered. With relative_scaling=1, a word that is twice as frequent will have twice the size. If you want to consider the word frequencies and not only their rank, relative_scaling around .5 often looks good. .. versionchanged: 2.0 Default is now 0.5. color_func : callable, default=None Callable with parameters word, font_size, position, orientation, font_path, random_state that returns a PIL color for each word. Overwrites "colormap". See colormap for specifying a matplotlib colormap instead. regexp : string or None (optional) Regular expression to split the input text into tokens in process_text. If None is specified, ``r"\w[\w']+"`` is used. collocations : bool, default=True Whether to include collocations (bigrams) of two words. .. versionadded: 2.0 colormap : string or matplotlib colormap, default="viridis" Matplotlib colormap to randomly draw colors from for each word. Ignored if "color_func" is specified. .. versionadded: 2.0 normalize_plurals : bool, default=True Whether to remove trailing 's' from words. If True and a word appears with and without a trailing 's', the one with trailing 's' is removed and its counts are added to the version without trailing 's' -- unless the word ends with 'ss'. Attributes ---------- ``words_`` : dict of string to float Word tokens with associated frequency. .. versionchanged: 2.0 ``words_`` is now a dictionary ``layout_`` : list of tuples (string, int, (int, int), int, color)) Encodes the fitted word cloud. Encodes for each word the string, font size, position, orientation and color. Notes ----- Larger canvases with make the code significantly slower. If you need a large word cloud, try a lower canvas size, and set the scale parameter. The algorithm might give more weight to the ranking of the words than their actual frequencies, depending on the ``max_font_size`` and the scaling heuristic.
https://github.com/amueller/word_cloud/
zhuan :http://blog.csdn.net/heyuexianzi/article/details/76851377?locationNum=6&fps=1