介绍个高大上的玩意:词云图
下面教大家如何制作词云图,代码很简单:
先给出代码:
import jieba
from wordcloud import WordCloud, ImageColorGenerator
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
with open('E:\\云图评论.txt', 'r', encoding="UTF-16") as file1:
content = "".join(file1.readlines())
content_after = "".join(jieba.cut(content, cut_all=True))
##添加的代码,把刚刚你保存好的图片用Image方法打开,
##然后用numpy转换了一下
images = Image.open("F:\\word_cloud\\2.jpg")
maskImages = np.array(images)
##修改了一下wordCloud参数,就是把这些数据整理成一个形状,
##具体的形状会适应你的图片的.
wc = WordCloud(font_path="msyh.ttc", background_color="black", max_words=1000, max_font_size=100, width=1500,
height=1500, mask=maskImages).generate(content)
plt.imshow(wc)
wc.to_file('2.jpg')
需要如下模块:
import jieba
from wordcloud import WordCloud, ImageColorGenerator
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
安装即可。
with open('E:\\云图评论.txt', 'r', encoding="UTF-16") as file1:
content = "".join(file1.readlines())
content_after = "".join(jieba.cut(content, cut_all=True))
为添加云图里面的内容为什么;即为你想要云图显示为啥,我这里使用的为之前用过的评论。
下面给出我这里使用的图片:
运行如图:
大家可以制作自己喜欢的图片。