#导入相应的库
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
#导入文本数据并惊醒简单的文本处理
#去掉换行符和空格
text = open('F:/a.txt',encoding='utf-8').read()
text = text.replace('n',"").replace("u3000","")
#分词,返回结果为词的列表
text_cut = jieba.lcut(text)
#将分好的词用某个符号分割开成连字符
text_cut = ' '.join(text_cut)
#导入停用词
stop_words = open('F:/stop_words_ch.txt',encoding='utf-8').read().split("n")
background = Image.open('F:/background1.png')
graph = np.array(background)
#使用WordCloud生成词云
word_cloud = WordCloud(font_path="simsun.ttc",
background_color="white",
mask=graph,
stopwords=stop_words)
word_cloud.generate(text_cut)
#运用matplotlib展示结果
plt.subplots(figsize=(12,8))
plt.imshow(word_cloud)
plt.axis("off")
plt.show()
我的结果长这样
停用词表,网上一大堆。随便哪个都行。



