生成词云图的代码

main
pw4izm9rs 2 months ago
parent 23e040b297
commit 2581a30f93

@ -0,0 +1,33 @@
import jieba
import wordcloud
import imageio
f = open("danmu.txt",encoding = 'utf-8')
text = f.read()
text_list = jieba.lcut(text)
text_str = ' '.join(text_list)
img = imageio.imread('五环.png')
# 制作方形词云图
wc1 = wordcloud.WordCloud(
width=500, # 设置宽度
height=500, # 设置高度
background_color='white', # 设置背景颜色
stopwords={'?', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''},# 屏蔽掉到一些没有意义的词
font_path='msyh.ttc', # 设置字体文件
)
wc1.generate(text_str)
wc1.to_file('WordCloud_1.png')
# 制作五环形状词云图
wc2 = wordcloud.WordCloud(
width = 500, # 设置宽度
height = 500, # 设置高度
background_color = 'white', # 设置背景颜色
mask = img, # 设置形状
stopwords = {'?','','','','','','','','','','','','','','','',''}, # 屏蔽掉到一些没有意义的词
font_path = 'msyh.ttc', # 设置字体文件
)
wc2.generate(text_str)
wc2.to_file('WordCloud_2.png')
Loading…
Cancel
Save