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技术干货文章。

相关推荐
天若有情6731 分钟前
【Python】什么是列表推导式?
开发语言·python
Bruce_Liuxiaowei1 小时前
基于Flask的勒索病毒应急响应平台架构设计与实践
后端·python·flask
Channing Lewis1 小时前
python headq包介绍
python
Freak嵌入式1 小时前
一文速通 Python 并行计算:06 Python 多线程编程-基于队列进行通信
开发语言·python·多线程·面向对象·并行计算
Elendill2 小时前
【算法笔记】并查集详解
笔记·python·算法
databook2 小时前
当决策树遇上脏数据:连续值与缺失值的解决方案
python·机器学习·scikit-learn
狗蛋不是狗2 小时前
Python 实现的运筹优化系统数学建模详解(最大最小化模型)
python·数学建模·优化算法·狗蛋不是狗·最大最小化模型
小白—人工智能2 小时前
数据可视化 —— 折线图应用(大全)
python·信息可视化·数据可视化
hvinsion2 小时前
从PPT到PNG:Python实现的高效PPT转图工具
python·powerpoint·ppt转图片
Aerkui2 小时前
Python面向对象-开闭原则(OCP)
开发语言·python·开闭原则