对于中文文本生成词云,可能会出现中文显示不出,显示为方框,经过测试发现只要在WordCloud的参数里添加 font_path='msyh.ttc’ 就可以
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
path = 'wordcloud.txt'
with open(path, encoding='utf-8') as f:
mytext = f.read()
#中文分词
text = ' '.join(jieba.cut(mytext))
#显示词云
wc = WordCloud(font_path='msyh.ttc').generate(text)
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()