I miss you。
Wordcloud 是Python第三方库中用于制作简单分词云图的第三方库,可以根据自己喜欢的颜色,喜欢的形状制作出美丽的词云图。
所谓的词云图,也叫文字云,是对文本中出现频率较高的“关键词”予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨。可以在每次的报告中迅速的找到核心词汇,掌握接下来发展的目的,方向。
建立一个file.txt的文本文件,把你要统计的文章保存的这个文件中,运行如下的代码就可以看到词云图啦
from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Read the whole text.
text = open('file.txt').read()
# Generate a word cloud image
wordcloud = WordCloud().generate(text)
# Display the generated image:
# the matplotlib way:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
效果如图
上面的词云图又丑有难看对不对,不要着急,这里可以自定义词云图的形状,自定义颜色。
在这里我的图片是一张心形
from os import path
from PIL import Image
import numpy as np
from wordcloud import WordCloud
import matplotlib.pyplot as plt
d=path.dirname('E:\\study\\jupyter notebook')
text=open(path.join(d,"constitution.txt")).read()
alice_mask = np.array(Image.open(path.join(d, "2.jpg")))
wordcloud=WordCloud(background_color="white",max_words=2000,mask=alice_mask)
wordcloud.generate(text)
wordcloud.to_file(path.join(d,"3.jpg"))
# 步骤4-1:创建一个图表画布
plt.figure(10)
# 步骤4-2:设置图片
plt.imshow(wordcloud, interpolation="bilinear")
# 步骤4-3:取消图表x、y轴
plt.axis("off")
# 显示图片
plt.show()
是不是很有趣呀!
还有很多有趣的东西等你发现呢