一般词云
代码:
import jieba
import wordcloud
f = open("中国特色社会主义.txt", "r")
t = f.read()
f.close()
Is = jieba.lcut(t)
txt = " ".join(Is)
w = wordcloud.WordCloud(font_path="msyh.ttc",\
width=1000,height=700,background_color="white",)
w.generate(txt)
w.to_file("grwordcloud.png")
效果:
特殊形状的词云.
此处涉及到另外一个第三方库,安装的时候注意版本
代码:
import jieba
import wordcloud
from scipy.misc import imread
mask = imread("wujiaoxing.png")
f = open("中国特色社会主义.txt", "r")
t = f.read()
f.close()
Is = jieba.lcut(t)
txt = " ".join(Is)
w = wordcloud.WordCloud(font_path="msyh.ttc",mask = mask,\
width=1000,height=700,background_color="white",)
w.generate(txt)
w.to_file("grwordcloud.png")
效果: