wordcloud默认是不支持显示中文的,中文会被显示成方框。
wordlcloud.WordCloud类初始化函数有个设置字体的参数font_path,把支持中文的字体的路径传给font_path。
如下:
from wordcloud import WordCloud
f = open('testfile.txt', 'r')
txt=f.read()
wordcloud = WordCloud(font_path="ygyxsziti2.0.ttf",background_color="black", width=800, height=860, margin=2).generate(txt)
wordcloud.to_file('test1.png')//保存图片
这样,不显示中文的问题就解决了。