#英文文本制作词云图
#pip install wordcloud
filename="XX.txt" # 将目标文本文件放入这里
with open(filename,encoding="utf8")as f:
mytext=f.read()
mytext #查看该变量中存储的文本
from wordcloud import WordCloud
wordcloud = WordCloud().generate(mytext)
import matplotlib.pyplot as plt
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off") #将成图的周围的刻度隐藏
#每次成图的内容不一定一致,但是大体上该出现的都会予以显示
#中文文本制作词云图(中文分词+中文词云制作)
#pip install jieba
filename = "XX-chinese.txt" #这里放入你的目标中文文本文件
with open(filename,encoding="utf-8")as f:
mytext = f.read()
print(mytext) #不同于英文文本,中文文本需要用到print()显示
import jieba
mytext=" ".join(jieba.cut(mytext)) #中文文本分词
print(mytext) #分词后显示查看
from wordcloud import WordCloud
wordcloud = WordCloud(font_path="simsun.ttf").generate(mytext) #由于词云包缺少中文字体,所以这里要指定汉字字体便于显示
import matplotlib.pyplot as plt
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")