#python 做词云 -jupyter跟随王树义教程学习 参考连接 https://www.jianshu.com/p/e4b24a734ccc https://www.jianshu.com/p/af6201d2f0c6
安装 wordcloud
pip install wordcloud
#进入包含txt文件的Jupyter
代码
记得库都要安装
from imageio import imread
import matplotlib.pyplot as plt
import os
from wordcloud import WordCloud, ImageColorGenerator
filename = "yes-minister.txt"
f = open(filename,encoding='utf-8')
mytext=f.read()
from wordcloud import WordCloud
wordcloud = WordCloud().generate(mytext)
import matplotlib.pyplot as plt
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis('off')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-idmOra1O-1628130214481)(D:/5bct/output_7_1.png)]
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
kust_coloring = np.array(Image.open("kust.jpg"))
wc = WordCloud( background_color="white", max_words=2000, mask=kust_coloring)
atext=open( "yes-minister.txt",encoding='utf-8').read()
image_colors = ImageColorGenerator(kust_coloring)
wc.generate(atext)
fig, axes = plt.subplots(1, 3)
axes[0].imshow(wc, interpolation="bilinear")
axes[1].imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
axes[2].imshow(kust_coloring, cmap=plt.cm.gray, interpolation="bilinear")
for ax in axes:
ax.set_axis_off()
plt.show()
? [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QGFCSfGm-1628130214484)(D:/5bct/output_16_0.png)]
: ax.set_axis_off() plt.show()
?
[外链图片转存中...(img-QGFCSfGm-1628130214484)]
|