Python 词云图
2022-11-06
工作需要,于是写了,写完整理,分享出来
目录结构
----main.py python源代码 ----stop.txt 过滤掉的词语 ----simhei.ttf 字体格式文件 ----text.txt 要生成词云图的txt文件 ----001.png 背景图
python源代码
import jieba from wordcloud import WordCloud import numpy as np from PIL import Image from matplotlib import colors f = open(r'text.txt',"r",encoding="utf-8") text = f.read() f.close() words_list_jieba = jieba.lcut(text) def findifhave(demo,stop): for ret in stop: if(demo == ret): return 'T' stop = ['\n'] with open("stop.txt",'r',encoding='utf-8') as f1: for line in f1: stop.append(line.replace("\n","")) f1.close() dict = {} for key in words_list_jieba: dict[key] = dict.get(key,0) + 1 for demo in list(dict.keys()): if('T' == findifhave(demo,stop)): del dict[demo] dict1 = sorted(dict.items(),key = lambda d:d[1] , reverse = True) print(dict1) background_image = np.array(Image.open('001.png')) colormaps = colors.ListedColormap(['#871A84', '#BC0F6A', '#BC0F60', '#CC5F6A', '#AC1F4A']) wordcloud = WordCloud(font_path='simhei.ttf', # 字体 prefer_horizontal=0.99, background_color='white', # 背景色 max_words=20, # 显示单词数 max_font_size=400, # 最大字号 stopwords=stop, # 过滤噪声词 mask=background_image, # 背景轮廓 colormap=colormaps, # 使用自定义颜色 collocations=False ).fit_words(dict) image = wordcloud.to_image() image.show() # 展示图片 wordcloud.to_file('词云图.png') # 保存图片
效果图
发表评论: