wordcloud,一个超酷的python库

微信公众号:愤怒的it男,超多Python技术干货文章。

一、简单介绍一下

词云图是文本挖掘中用来表征词频的数据可视化图像,通过它可以很直观地展现文本数据中地高频词,让读者能够从大量文本数据中快速抓住重点。如下图:

wordcloud则是一个非常优秀的词云展示python库,它支持自定义词云图的大小、颜色、字体等,甚至可以通过蒙版图片设置词云图的形状。因此,我们可以借助wordcloud轻松生成精美的词云图。

二、安装只需一行命令

python 复制代码
pip install wordcloud

三、从一个简单例子开始

python 复制代码
from wordcloud import WordCloud

text = "微信公众号:愤怒的it男"

wc = WordCloud(font_path='FZYTK.TTF', repeat=True)
wc.generate(text)
wc.to_file('wordcloud.png')

这里通过WordCloud类设置字体为方正姚体,背景颜色为白色,文本可以重复显示。生成WordCloud对象后,使用generate()方法将"微信公众号:愤怒的it男"生成词云图。最后,使用to_file()方法生成图片文件。

四、细说wordcloud

WordCloud作为wordcloud库最核心的类,其主要参数及说明如下:

这里以wordcloud库官方文档的constitution.txt文件作为数据,覆盖WordCloud类的各种参数设置用法,绘制出一张精美的词云图。

首先,读入constitution.txt数据,并将数据清洗成空格分隔的长字符串。

python 复制代码
import re

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

print(text[:500])

然后,在默认参数设置下,使用WordCloud对象的generate()和to_file()方法生成一张简单的词云图。

python 复制代码
from wordcloud import WordCloud
import re

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

wc = WordCloud()
wc.generate(text)

wc.to_file('wordcloud.png')

以上词云图是在默认参数下生成的,简单粗糙不好看。接下来我们将对WordCloud的各种参数调整设置,不断地对以上词云图进行升级改造。

1、设置图片属性

设置图片宽为600,高为300,放大1.5倍,色彩空间为RGBA,背景颜色为None。

python 复制代码
from wordcloud import WordCloud
import re

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

wc = WordCloud(
    width=600,
    height=300,
    scale=1.5,
    mode='RGBA',
    background_color=None,
    )
wc.generate(text)

wc.to_file('wordcloud.png')

2、设置文字布局

设置水平比例为1(即全部为水平文字),最多只显示100个词,停用词使用自带的词典(中文需要传入自定义的),相关一致性为0.3,文字布局为非随机,不允许重复词。

python 复制代码
from wordcloud import WordCloud
import re

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

wc = WordCloud(
    width=600,
    height=300,
    scale=1.5,
    mode='RGBA',
    background_color=None,
    prefer_horizontal=1,
    max_words=400,
    stopwords=None,
    relative_scaling=0.3,
    random_state=4,
    repeat=False,
    )
wc.generate(text)

wc.to_file('wordcloud.png')

3、设置字体属性

设置字体为'JOKERMAN.TTF',最小字号为2,最大字号为150。

python 复制代码
from wordcloud import WordCloud
import re

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

wc = WordCloud(
    width=600,
    height=300,
    scale=1.5,
    mode='RGBA',
    background_color=None,
    prefer_horizontal=1,
    max_words=400,
    stopwords=None,
    relative_scaling=0.3,
    random_state=4,
    repeat=False,
    font_path='JOKERMAN.TTF',
    min_font_size=2,
    max_font_size=150,
    )
wc.generate(text)

wc.to_file('wordcloud.png')

4、设置蒙版

设置微信公众号【愤怒的it男】头像的黑白图片为蒙版图片。

python 复制代码
from PIL import Image
from wordcloud import WordCloud
import numpy as np
import re

mask_picture = np.array(Image.open('angry_it_man_mask.png'))

with open('constitution.txt') as c:
    text = ' '.join([word.group().lower() for word in re.finditer('[a-zA-Z]+', c.read())])

wc = WordCloud(
    width=600,
    height=300,
    scale=1.5,
    mode='RGBA',
    background_color=None,
    prefer_horizontal=1,
    max_words=400,
    stopwords=None,
    relative_scaling=0.3,
    random_state=4,
    repeat=False,
    font_path='JOKERMAN.TTF',
    min_font_size=2,
    max_font_size=150,
    mask=mask_picture,
    )
wc.generate(text)

wc.to_file('wordcloud.png')

微信公众号:愤怒的it男,超多Python技术干货文章。

相关推荐
费弗里29 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(3)
python·dash
dme.1 小时前
Javascript之DOM操作
开发语言·javascript·爬虫·python·ecmascript
加油吧zkf1 小时前
AI大模型如何重塑软件开发流程?——结合目标检测的深度实践与代码示例
开发语言·图像处理·人工智能·python·yolo
t_hj1 小时前
python规划
python
czhc11400756631 小时前
Linux 76 rsync
linux·运维·python
悠悠小茉莉2 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
m0_625686552 小时前
day53
python
Real_man2 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流
python
站大爷IP3 小时前
Python文件操作的"保险箱":with语句深度实战指南
python
运器1233 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程