词云的使用
需要先下载jieba库和wordcloud库
import jieba
import wordcloud
f=open(“文件的名字.txt”,“r”,encoding=“utf-8”)
t=f.read()
f.close()
ls=jieba.lcut(t)
txt=" ".join(ls)
w=wordcloud.WordCloud( font_path=“msyh.ttc”,
width=1000,height=700,background_color=“white”,
max_words=15) #msyh.ttc是微软雅黑字体,宽1000,长700像素,背景白色,最多生成单词15个
w.generate(txt)
w.to_file(“图片名字自己取.png”)
也可以把背景变成五角星,变得更有型。
from scipy.misc import imread
import jieba
import wordcloud
mask=imread(“fivestart.png”)
f=open(“文件的名字.txt”,“r”,encoding=“utf-8”)
t=f.read()
f.close()
ls=jieba.lcut(t)
txt=" ".join(ls)
w=wordcloud.WordCloud( font_path=“msyh.ttc”,mask=mask
width=1000,height=700,background_color=“white”,
max_words=15) #这里多了一个mask=mask
w.generate(txt)
w.to_file(“图片名字自己取.png”)