简介:
词云,又称文字云,是文本数据的视觉表示,由词汇组成类似云的彩色图形,用于展示大量文本数据。通常用于描述网站上的关键字元数据(标签),或可视化自由格式文本。 每个词的重要性以字体大小或颜色显示。 词云的作用:
- 快速感知最突出的文字
- 快速定位按字母顺序排列的文字中相对突出的部分
词云的本质是点图,是在相应坐标点绘制具有特定样式的文字的结果
依赖:
- 因为用python画图所以用引用matplotlib库
- 因为要分词所以要引用分词库 这里用中文分词jieba(自称中文最好的分词工具)
- 因为要制作词云所以引用开源词云wordcloud库
- 如果没有意思库的话 自行 pip install 或通过其他方式下载
注意事项
-
因为wordcloud是国外人开发的词云库,所以默认中文乱码,因此需要引入中文字体
-
WordCloud构造函数可以看到有个字体选项
image.png
-
网上搜索了一下这个地方ttf格式的中文字体可以用
wget http://labfile.oss.aliyuncs.com/courses/756/DroidSansFallbackFull.ttf
代码演示:
# -*- coding: utf-8 -*- # @Time : 2019/2/15 22:10 # @Author : Chaucer_Gxm # @Email : gxm4167235@163.com # @File : Word_1.py # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm # @blog : https://blog.csdn.net/qq_24819773 # @Software: PyCharm from wordcloud import WordCloud import matplotlib.pyplot as plt # 1.打开文本 file = open('constitution.txt').read() # 2.生成图像 wc = WordCloud().generate(file) # 3.显示词云 plt.imshow(wc, interpolation='bilinear') plt.axis('off') plt.show() # 4.保存到文件 wc.to_file('wordcloud.png')
# -*- coding: utf-8 -*- # @Time : 2019/2/15 22:50 # @Author : Chaucer_Gxm # @Email : gxm4167235@163.com # @File : Wordcloud_2_chinese3.py # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm # @blog : https://blog.csdn.net/qq_24819773 # @Software: PyCharm from wordcloud import WordCloud from PIL import Image import matplotlib.pyplot as plt import numpy as np import jieba # 1.读取文件 file = open('xyj.txt', 'rb').read() # 2.中文分词 text = ' '.join(jieba.cut(file)) print(text[:200]) # 3.生成对象及 mask mask = np.array(Image.open('black_mask.png')) wc = WordCloud(mask=mask, font_path='Hiragino.ttf', mode='RGBA', background_color=None).generate(text) # 4.显示词云 plt.imshow(wc, interpolation='bilinear') plt.axis('off') plt.show() # 5.保存到文件 wc.to_file('Wordcloud_chinese3.png')
# -*- coding: utf-8 -*- # @Time : 2019/2/15 23:20 # @Author : Chaucer_Gxm # @Email : gxm4167235@163.com # @File : Wordcloud_2_chinese5.py # @GitHub : https://github.com/Chaucergit/Code-and-Algorithm # @blog : https://blog.csdn.net/qq_24819773 # @Software: PyCharm from wordcloud import WordCloud, ImageColorGenerator from PIL import Image import numpy as np import matplotlib.pyplot as plt import jieba import random # 1.读取文件 file = open('xyj.txt', 'rb').read() # 2.中文分词 text = ' '.join(jieba.cut(file)) print(text[:300]) # 颜色函数 def random_color(word, font_size, position, orientation, font_path, random_state): s = 'hsl(0, %d%%, %d%%)' % (random.randint(60, 80), random.randint(60, 80)) print(s) return s # 3.生成 mask 及词云对象 mask = np.array(Image.open('color_mask.png')) wc = WordCloud(color_func=random_color, mask=mask, font_path='Hiragino.ttf', width=1000, height=800, mode='RGBA', background_color=None).generate(text) # 4.显示词云 plt.imshow(wc, interpolation='bilinear') plt.axis('off') plt.show() # 5.保存图像 wc.to_file('Wordcloud_chinese5.png')
运行结果:
-
-
全部文件链接(github)
- https://github.com/Chaucergit/Code-and-Algorithm/tree/master/DL/1.%E8%AF%8D%E4%BA%91
-
参考书籍及网络资源:
[1].深度有趣[2].https://www.jianshu.com/p/af4b3c2c05d2