需要使用到的各类库
- PIL
- matplotlib
- numpy
- wordcloud
首先是需要安装wordcloud
pip install wordcloud
即可安装最新的wordcloud库
开始构成词云
- 读取需要用来构成词云的文字
#使用了《爱丽丝梦游仙境》作为词的来源
with open("alice.txt") as f:
text=f.readlines()
text_clean=[]
for line in text:
line=line.strip()
text_clean.append(line.replace("\n",""))
text="".join(text_clean)
- 读取需要用来做mask的图片(可选)
import numpy as np
import PIL.Image as Image
coloring=np.array(Image.open("path"))
- 构建word_cloud对象的实例
from wordcloud import WordCloud,ImageColorGenerator
my_wordcloud=WordCloud(backgroud_color=" ",max_words=任意数字,
mask=上一步的coloring,max_font_size=自己设定,random_state=变化的范围,scale=生成图片和原图的大小倍数,font_path=字体路径)
#example
my_wordcloud = WordCloud(background_color="white", max_words=20000,mask=coloring,
max_font_size=40, random_state=42, scale=2,
font_path=" Fonts/Hannotate TC/Hannotate.ttc")
- 构成词云
#构成词云
my_wordcloud.generator(text)
- 绘制出词云
import matplotlib.pyplot as plt
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
默认颜色的词云
#使用图片背景颜色构成词云
image_colors=ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
#plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()