python 绘制词云图(自定义png形状、指定字体、颜色)最全!!!
创始人
2025-01-09 17:07:04
0

前言

本文为分总结构,有特定需求的可以查阅前部分分结构的对应板块,最后的总结不懂的可以在分板块查阅解释。分板块分别有引用的库、阅读文本、分词并设置停用词、设置png掩膜、字体设置、生成词云图,感谢您点开这篇分享,祝顺利。

目录

前言

一、引用的库

二、阅读文本(让python阅读)

三、分词并设置停用词

四、设置png掩膜

五、字体设置

六、生成词云图

总结

 生成示例

一、引用的库

from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt import numpy as np import jieba.posseg as pseg from collections import Counter import PIL.Image as Image from matplotlib import colors

确保已安装以上的库,不然运行会报错

#安装库可以用清华的镜像网站(可能会更新,可以上官网查询地址)

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

二、阅读文本(让python阅读)

# 阅读文本(这里yourfile.txt,根据文本所在具体位置进行设置) text = open("yourfile.txt", encoding="utf-8").read()   words = pseg.cut(text)

这里"yourfile.txt",根据文本所在具体位置进行设置

三、分词并设置停用词

# 按指定长度和词性提取词 report_words = [] for word, flag in words:     if (len(word) >= 2) and ('n' in flag): #这里设置统计的字数         report_words.append(word)  # 设置停用词 stopwords = set(STOPWORDS) stopwords.update(["的", "感谢", "我代表", "以上", "报告", "表示诚挚感谢","战略"])  # 去除停用词 report_words = [word for word in report_words if word not in stopwords]  # 统计高频词汇 result = Counter(report_words).most_common(200) #词的个数  # 建立词汇字典 content = dict(result) #输出词频统计结果 for i in range(50):     word,flag=result[i]     print("{0:<10}{1:>5}".format(word,flag))  

len(word)设置的是词长,想要提取两个两个字的就设置2,三个三个字的就设置3(以此类推)

result = Counter(report_words).most_common(200)  这里的200是指统计200个词用于绘制,可以根据需求设置

四、设置png掩膜

#设置png掩膜(yourfile.png根据实际路径进行替换) background = Image.open("yourfile.png") mask = np.array(background)

yourfile.png根据实际路径进行替换

如果输出结构还是长方形(正方形),应该是png图片“有问题”,可以尝试以下的处理

1.用p图软件把图片改成纯黑色(可能别的也可以,我没试过)

2.用以下代码把白色背景改为透明

# 如果当前位深是32的话,可以不用写转RGBA模式的这一句,但是写上也没啥问题 # 从RGB(24位)模式转成RGBA(32位)模式 img = Image.open("yourfile.png").convert('RGBA') W, L = img.size white_pixel = (0, 0, 0, 0)  # 白色 for h in range(W):     for i in range(L):         if img.getpixel((h, i)) == white_pixel:             img.putpixel((h, i), (255, 255, 255, 0))  # 设置透明 img.save("yourfile_new.png")  # 自己设置保存地址

这里有两个参数需要修改

yourfile.png根据实际路径进行替换,yourfile_new.png(这是修改后图片)根据实际路径进行替换

五、字体设置

# 设置字体样式路径 font_path = r"C:\Windows\Fonts\STLITI.TTF"  # 设置字体大小 max_font_size =200 min_font_size =10  # 建立颜色数组,可更改颜色 color_list = ['#FF274B'] # 调用颜色数组 colormap = colors.ListedColormap(color_list)

字体样式:一般都在这个路径,可以自己根据需求修改或者下载想要的字体

字体大小:最大和最小根据需求更改

字体颜色:可以不要这行代码(默认设置),也可以根据需求设置一种或多种颜色(我这里只设置了一种)

六、生成词云图

# 生成词云 wordcloud = WordCloud(scale=4,                         #输出清晰度                       font_path=font_path,             #输出路径                       colormap=colormap,               #字体颜色                       width=1600,                      #输出图片宽度                       height=900,                      #输出图片高度                       background_color='white',        #图片背景颜色                       stopwords=stopwords,             #停用词                       mask=mask,                       #掩膜                       max_font_size=max_font_size,     #最大字体大小                       min_font_size=min_font_size)     #最小字体大小 wordcloud.generate_from_frequencies(content)  # 使用 matplotlib 显示词云 plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show() # 保存词云图 wordcloud.to_file("wordcloud.png")

如果前面参数都是按着我的来设置的话,这里直接复制粘贴就好

总结

from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt import numpy as np import jieba.posseg as pseg from collections import Counter import PIL.Image as Image from matplotlib import colors  # 阅读文本(这里yourfile.txt,根据文本所在具体位置进行设置) text = open("yourfile.txt", encoding="utf-8").read() words = pseg.cut(text)  # 按指定长度和词性提取词 report_words = [] for word, flag in words:     if (len(word) >= 2) and ('n' in flag): #这里设置统计的字数         report_words.append(word)  # 统计高频词汇 result = Counter(report_words).most_common(200) #词的个数  # 建立词汇字典 content = dict(result) #输出词频统计结果 for i in range(50):     word,flag=result[i]     print("{0:<10}{1:>5}".format(word,flag))  # 设置停用词 stopwords = set(STOPWORDS) stopwords.update(["的", "感谢", "我代表", "以上", "报告", "表示诚挚感谢","战略"])  #设置png掩膜(yourfile.png根据实际路径进行替换) background = Image.open("yourfile.png").convert('RGB') mask = np.array(background) ''' # 如果当前位深是32的话,可以不用写转RGBA模式的这一句,但是写上也没啥问题 # 从RGB(24位)模式转成RGBA(32位)模式 img = Image.open("yourfile.png").convert('RGBA') W, L = img.size white_pixel = (0, 0, 0, 0)  # 白色 for h in range(W):     for i in range(L):         if img.getpixel((h, i)) == white_pixel:             img.putpixel((h, i), (255, 255, 255, 0))  # 设置透明 img.save("yourfile_new.png")  # 自己设置保存地址 ''' # 设置字体样式路径 font_path = r"C:\Windows\Fonts\STLITI.TTF"  # 设置字体大小 max_font_size =200 min_font_size =10  # 建立颜色数组,可更改颜色 color_list = ['#FF274B'] # 调用颜色数组 colormap = colors.ListedColormap(color_list)  # 生成词云 wordcloud = WordCloud(scale=4,                         #输出清晰度                       font_path=font_path,             #输出路径                       colormap=colormap,               #字体颜色                       width=1600,                      #输出图片宽度                       height=900,                      #输出图片高度                       background_color='white',        #图片背景颜色                       stopwords=stopwords,             #停用词                       mask=mask,                       #掩膜                       max_font_size=max_font_size,     #最大字体大小                       min_font_size=min_font_size)     #最小字体大小 wordcloud.generate_from_frequencies(content)  # 使用 matplotlib 显示词云 plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show() # 保存词云图 wordcloud.to_file("wordcloud.png") 

 生成示例

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...