You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
2 months ago
|
# 生成云图
|
||
|
|
||
|
|
||
|
def make_graph():
|
||
|
text_data = ''
|
||
|
with open('AI_danmu.txt', 'r', encoding='utf-8') as file:
|
||
|
for line in file:
|
||
|
text_data += line.strip() + ' '
|
||
|
|
||
|
# 使用jieba进行中文分词
|
||
|
words = jieba.cut(text_data, cut_all=False)
|
||
|
word_list = " ".join(words) #列表转成字符串
|
||
|
|
||
|
# 加载自定义形状图片
|
||
|
shape_mask = np.array(Image.open('img.png'))
|
||
|
|
||
|
# 创建词云图对象,并设置形状
|
||
|
wordcloud = WordCloud(width=2000,
|
||
|
background_color='white',
|
||
|
mask=shape_mask, # 使用自定义形状
|
||
|
contour_width=1,
|
||
|
contour_color='white', # 边框颜色
|
||
|
font_path='STKAITI.TTF', # 用于中文显示的字体文件
|
||
|
max_words=30000, # 最多显示的词语数量
|
||
|
colormap='Blues', # 颜色映射,可以根据需要更改
|
||
|
).generate(word_list)
|
||
|
|
||
|
# 使用形状图片的颜色
|
||
|
image_colors = ImageColorGenerator(shape_mask)
|
||
|
wordcloud.recolor(color_func=image_colors)
|
||
|
|
||
|
# 显示词云图
|
||
|
plt.figure(figsize=(10, 5))
|
||
|
plt.imshow(wordcloud, interpolation='bilinear')
|
||
|
plt.axis('off') # 隐藏坐标轴
|
||
|
plt.title('')
|
||
|
plt.show()
|