import os
from os import path
from wordcloud import WordCloud
from matplotlib import pyplot as plt
#获取当前文件路径
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
#获取文本
text = open(path.join(d,'lengend1900.txt')).read()
#生成云词
wc = WordCloud(scale=2,max_font_size=100)
wc.generate_from_text(text)
#显示图像
plt.imshow(wc,interpolation='bilinear')
plt.axis('off')
plt.tight_layout()
#存储图像
wc.to_file('1900_basic.png')
plt.show()
运行结果: