python词云制作
作者:互联网
结果图片:
代码如下:
import wordcloud
import matplotlib
import jieba
with open("一个广告人的自白.txt",'r',encoding='utf-8') as f:
renmin=f.read()
jieba.load_userdict("一个广告人的自白.txt")
seg_list=jieba.cut(renmin,cut_all=False)
tf={}
for seg in seg_list:
if seg in tf:
tf[seg]+=1
else:
tf[seg]=1
ci=list(tf.keys())
with open('中文停用.txt','r',encoding='utf-8') as ft:
stopword=ft.read()
for seg in ci:
if tf[seg]<5 or len(seg)<2 or seg in stopword or "一" in seg:
tf.pop(seg)
print(tf)
from PIL import Image
import numpy as np
mask=np.array(Image.open("广告人.png"))
import os
print(os.getcwd())
from wordcloud import WordCloud
import matplotlib.pyplot as plt
wc=WordCloud(font_path=font,width=600,height=800,mask=mask).generate_from_frequencies(tf)
plt.imshow(wc)
plt.axis("off")
plt.show()
wc.to_file("广告人的自白.jpg")
心得与收获:
1.在安装各类包的时候出现阻碍,可以仔细研究报错内容,搜索网上有没有类似问题的解决方法,一步一步解决。
2.在文件的命名和存储路径上要多留意,不然会出现意想不到的错误!
标签:plt,python,广告人,list,seg,词云,tf,import,制作 来源: https://blog.csdn.net/weixin_57434133/article/details/120314510