python wordcloud的使用
2017-01-10 18:02
549 查看
1、Wordcloud的安装
pip install wordcloud
github下载并解压
2
3
4
安装依赖包
安装wordcloud
出现以下情况:
![](http://img.blog.csdn.net/20170110174510223?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
从https://www.microsoft.com/en-us/download/details.aspx?id=44266下载Microsoft
Visual C++ Compiler for Python 2.7
![](http://img.blog.csdn.net/20170110174843989?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
安装以下即可,接下来遇到什么安装包没安装的,在cmd环境下直接pip install
jieba(工具包名)
结果如下:
![](http://img.blog.csdn.net/20170110180201541?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
需要下载一下MSYH.TTF字体:网址http://www.121down.com/soft/softview-31524.html
![](http://img.blog.csdn.net/20170110180216154?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170110180221870?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170110180226310?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFveWFuMjAxNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
程序中有可能出现这种情况:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 108:
解决方法是在文件开头加上:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
方法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-master1
2
3
4
安装依赖包
sudo pip install -r requirements.txt1
安装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动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- 通过构建一个简单的掷骰子游戏去学习怎么用 Python 编程