词云图生成

代码段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import jieba
from wordcloud import WordCloud
from matplotlib.pyplot import imread

text = open("2020政府报告.txt").read()
font = "simhei.ttf"

# 背景图片
mask = imread("background.jpg")

cut = jieba.cut(text) # text为你需要分词的字符串/句子
result = ' '.join(cut) # 将分开的词用空格连接

# 剔除掉的关键词
exclude = {'我们', '今年', '同志', '我国'}

wc = WordCloud(collocations=False, # 避免重复单词
font_path=font, # 设置字体
mask=mask, # 设置背景
background_color="white",
# width=1400, height=1400, margin=2, # 图像宽高,字间距
stopwords=exclude)
wc.generate(result)
wc.to_file('2020政府报告.png')

引用模块

import jieba:中文词解析。
from wordcloud import WordCloud:词云图模块,用于生成词云图。
from matplotlib.pyplot import imread:图片加载。

数据源

background
2020政府报告

运行结果

设置背景版本

默认版本

作者

Etsu

发布于

2020-02-02

更新于

2020-02-02

许可协议

评论