Python词云

Python生成词云的代码

#词云
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("图片源文件路径及名称")  #添加背景为白色的图形做为词云背景
excludes = { }  #此处添加不想在词云中显示的词
f = open("txt文件路径及名称","r", encoding="GB2312")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud( font_path = "msyh.ttc", mask=mask, \
                         width=2000, height= 1400, background_color="white",\
                         )
w.generate(txt)
w.to_file("grwordcloud.png") 

你可能感兴趣的:(Python笔记)