Python制作词图云
1.在cmd安装三个库
pip install jieba
pip install matplotlib
pip install wordcloud
在PyCharm安装库,按File–>Settings–>Project:–>Project Interpreter–>点击+后会弹出页面
查找wordcloud库–>然后点击wordcloudku–>再点Install Package -->弹出installed successfully才安装成功
2.编写代码
import jieba
from wordcloud import WordCloud,ImageColorGenerator
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
#按着demo.png生成词图云
def start():
with open('temp.txt','r',encoding="UTF-8") as file1:
content = "".join(file1.readlines())
content_after = "".join(jieba.cut(content,cut_all=True))
##添加的代码,把刚刚你保存好的图片用Image方法打开,
##然后用numpy转换了一下
images = Image.open("demo.png")
maskImages = np.array(images)
##修改了一下wordCloud参数,就是把这些数据整理成一个形状,
##具体的形状会适应你的图片的.
wc = WordCloud(font_path="C:\\Windows\\Fonts\\simkai.ttf",background_color="white",max_words=1000,max_font_size=100,width=1500,height=1500,mask=maskImages).generate(content)
plt.imshow(wc)
wc.to_file('demo_result.png')
start()
2.说明
txt文件: