您的位置:首页 > 编程语言 > Python开发

python wordcloud的使用

2017-01-10 18:02 549 查看
1、Wordcloud的安装


方法1

pip install wordcloud


方法2

github下载并解压
wget  https://github.com/amueller/word_cloud/archive/master.zip
unzip master.zip
rm master.zip
cd word_cloud-master
1
2
3
4

安装依赖包
sudo pip install -r requirements.txt
1

安装wordcloud
python setup.py install

出现以下情况:



https://www.microsoft.com/en-us/download/details.aspx?id=44266下载Microsoft
Visual C++ Compiler for Python 2.7



安装以下即可,接下来遇到什么安装包没安装的,在cmd环境下直接pip install
jieba(工具包名)



结果如下:



需要下载一下MSYH.TTF字体:网址http://www.121down.com/soft/softview-31524.html

from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 获取当前文件路径
# __file__ 为当前文件, 在ide中运行此行会报错,可改为
# d = path.dirname('.')
d = path.dirname(__file__)

# 读取文本 alice.txt 在包文件的example目录下
#内容为

"""
Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll

This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.  You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org
'
"""

text = open(path.join(d, 'alice.txt')).read()

# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010 # 设置背景图片
alice_coloring = imread(path.join(d, "2222.png"))

wc = WordCloud(background_color="white", #背景颜色max_words=2000,# 词云显示的最大词数
mask=alice_coloring,#设置背景图片
stopwords=STOPWORDS.add("said"),
max_font_size=40, #字体最大值
random_state=42)
# 生成词云, 可以用generate输入全部文本(中文不好分词),也可以我们计算好词频后使用generate_from_frequencies函数
wc.generate(text)
# wc.generate_from_frequencies(txt_freq)
# txt_freq例子为[('词a', 100),('词b', 90),('词c', 80)]
# 从背景图片生成颜色值
image_colors = ImageColorGenerator(alice_coloring)

# 以下代码显示图片
plt.imshow(wc)
plt.axis("off")
# 绘制词云
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis("off")
# 绘制背景图片为颜色的图片
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.axis("off")
plt.show()
# 保存图片
wc.to_file(path.join(d, "名称.png"))







程序中有可能出现这种情况:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 108:

解决方法是在文件开头加上:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python wordcloud