forked from javasmall/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud.py
More file actions
26 lines (24 loc) · 1.08 KB
/
cloud.py
File metadata and controls
26 lines (24 loc) · 1.08 KB
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
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba.analyse
txt1 = "故宫的著名景点包括乾清宫、太和殿和午门等。其中乾清宫非常精美,午门是紫禁城的正门,午门居中向阳。"
#txt1 = open('word.txt', 'r', encoding='utf8').read() # word.txt,随便放点中文文章
#words_ls = jieba.cut(txt1, cut_all=False)
#words_split = " ".join(words_ls)
ags = jieba.analyse.extract_tags(txt1, topK=10)
text=" ".join(ags)
print(text)
#print(words_split)
wc = WordCloud(background_color="white",
width=1000,height=600,
min_font_size=20,
font_path="simhei.ttf",
#max_font_size = 200, # 设置字体最大值
random_state = 30, # 设置有多少种随机生成状态,即有多少种配色方案
) # 字体这里有个坑,一定要设这个参数。否则会显示一堆小方框wc.font_path="simhei.ttf" # 黑体
#wc.font_path="simhei.ttf"
my_wordcloud = wc.generate(text)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
wc.to_file('zzz.png') # 保存图片文件