微信公众号:愤怒的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技术干货文章。