获取词云的代码
import jieba
import matplotlib.pyplot as plt
from collections import Counter
import jieba.posseg as psg
from scipy.misc import imread
from wordcloud import WordCloud, ImageColorGenerator
artic = open('2.txt', 'r', encoding='utf8').read()
r='!.:;\n?“。,……”\u3000:的!, '
for i in r:
artic = artic.replace(i, '')
cut =','.join(jieba.cut(artic))
lcut = jieba.lcut(artic)
count = Counter(lcut).most_common(20)
print(cut)
path_of_font = './arialuni.ttf'
bg_path = 'C:/Users/Administrator/Pictures/map1.jpg'
bg_img = imread(bg_path)
my_wordcloud = WordCloud(font_path=path_of_font,max_font_size=100,mask=bg_img).generate(cut)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
效果图: