-
Notifications
You must be signed in to change notification settings - Fork 98
/
plotWordcloud.py
36 lines (30 loc) · 1.12 KB
/
plotWordcloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding:utf-8
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
def generate_wordcloud(text):
'''
输入文本生成词云,如果是中文文本需要先进行分词处理
'''
# 设置显示方式
d=path.dirname(__file__)
alice_mask = np.array(Image.open(path.join(d, "Images//alice_mask.png")))
font_path=path.join(d,"font//msyh.ttf")
stopwords = set(STOPWORDS)
wc = WordCloud(background_color="white",# 设置背景颜色
max_words=2000, # 词云显示的最大词数
mask=alice_mask,# 设置背景图片
stopwords=stopwords, # 设置停用词
font_path=font_path, # 兼容中文字体,不然中文会显示乱码
)
# 生成词云
wc.generate(text)
# 生成的词云图像保存到本地
wc.to_file(path.join(d, "Images//alice.png"))
# 显示图像
plt.imshow(wc, interpolation='bilinear')
# interpolation='bilinear' 表示插值方法为双线性插值
plt.axis("off")# 关掉图像的坐标
plt.show()