python-wordcloud词云

导入模块

python 复制代码
from wordcloud import WordCloud
import jieba
import imageio
import matplotlib.pyplot as plt
from PIL import ImageGrab
import numpy as np

wordcloud以空格为分隔符号,来将文本分隔成单词

PIL pillow模块

python 复制代码
img = imageio.imread('image.png')

这行代码使用imageio库读取一个名为"image.png"的图像文件,并将图像作为numpy数组存储在变量"img"中

dir可以查看一些东西

WordCloud对象创建的常用参数

  • font_path:字体文件的路径 - - - 默认None
  • widthheight:词云生成图片的宽高 - - - 默认宽400px,高200px
  • mask:词云形状 - - -默认None(方形图)
  • min_font_sizemax_font_size:词云中最大最小的字体字号 - - - 最小4号 最大根据高度自动调节
  • font_step:字号步进间隔 - - - 默认1
  • max_words:最大次数 - - - 200
  • stopwords:被排除的词列表,排除词不在词云中显示 - - - stopwords={'python'}
  • background_color:图片背景色 - - - 黑色
  • repeat=True:词太少时可以让词重复出现在词云中
  • contour_widthcontour_color:添加词云边框和边框颜色
  • colormap:修改字体颜色
    Matplotlib附带的色彩映射参考

WordCloud类的常用方法

  • generate(text):由text文本生成词云
  • to_file(filename):将词云图保存为名为filename的文件
  • to_image() :可以直接在jupyter里面看到词云的图片

案例

python 复制代码
from wordcloud import WordCloud

w = WordCloud()
w.generate('hi hi hello hi hi hello world!')
w.to_file('hi.png')
python 复制代码
import wordcloud

w = wordcloud.WordCloud(background_color='white',repeat=True)
text = 'hi,hello world!'

w.generate(text) 
w.to_image()
python 复制代码
w = wordcloud.WordCloud(background_color='white',repeat=True,colormap='PuRd_r')
python 复制代码
mask = np.array(PIL.Image.open('aixin.png'))
w = wordcloud.WordCloud(mask=mask,background_color='white',repeat=True,colormap='PuRd_r')

默认mask表示为binary(二进制)

对应参数是numpy 中的 array数组,将图片用PIL库打开 使用矩阵表示出来(图像本质就是矩阵)

python 复制代码
mask = np.array(PIL.Image.open('aixin.png'))
w = wordcloud.WordCloud(mask=mask,background_color='white',repeat=True,colormap='RdBu',contour_color='black',contour_width=5)
python 复制代码
w = wordcloud.WordCloud(mode='RGBA',mask=mask,background_color='white',repeat=True,colormap='RdBu')

mode='RGBA' 保存的图片不能为.jpg后缀,可以使用png

python 复制代码
from wordcloud import WordCloud
import imageio
import matplotlib.pyplot as plt

mk = imageio.imread('aixin.png')  # 打开图片文件
w = WordCloud(mask=mk,background_color='lightpink',font_path='msyh.ttc',colormap='Accent',min_font_size=2,stopwords={'就在这时'}) # msyh微软雅黑字体
f = open('data.txt','r',encoding='utf-8')
w.generate(f.read())
plt.imshow(w)   # 显示词云
plt.axis('off') # 隐藏坐标轴
plt.show()
w.to_file('aixincy.png') # 保存的词云图片大小和mask图片的大小一样

w.generate(" ".join(jieba.lcut(txt)))即为用空格的方法去分隔jieba库精确模式下形成的字符串。

jieba自带的词库包括:

  1. dict.txt.big - 大型词库,包含约2.7万个词汇和常用词语

  2. dict.txt.small - 小型词库,包含约1.4万个词汇和常用词语

  3. user.dict - 用户自定义词库,用户可以将自己的词汇添加到此文件中

  4. stop_words.txt - 停用词词典,包含约1000个常用停用词

  5. idf.txt - 关键词权重词典,用于提取文本中的关键词

  6. stop_words_cn.txt - 中文停用词词典,包含约1500个常用停用词

  7. stopwords.txt - 英文停用词词典,包含约400个常用停用词jieba自带的词库包括:

相关推荐
leisoo80974 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
WangYan20224 小时前
基于XGBoost与AI的生态—地学多源数据建模:植被与土地利用识别、土壤碳氮空间预测、生物多样性驱动机制、土壤微生物功能预测、生态退化与风险识别
python·机器学习·xgboost
金銀銅鐵6 小时前
[Python] 借助 turtle 逐字展示唐诗《金缕衣》
python
Python大数据分析@8 小时前
使用大模型MCP采集数据,爬虫已经无门槛
python·网络爬虫
quantdash_cc8 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
65岁退休Coder9 小时前
LangChain v1.3.4 笔记 - 07 补充:链式调用 LCEL
后端·python·langchain
卷无止境9 小时前
FastAPI 部署在 Nginx 后面到底该怎么配
后端·python
半亩码田9 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx19801210 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
Python私教10 小时前
签名 URL 刷新导致审批失效?别急着删掉所有 query
python·安全