From 9c9d893f3d1576862a774d235027a5bff73c1c7e Mon Sep 17 00:00:00 2001 From: p4oufk2iw <2488672761@qq.com> Date: Wed, 18 Sep 2024 03:39:39 +0800 Subject: [PATCH] ADD file via upload --- make_graph.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 make_graph.py diff --git a/make_graph.py b/make_graph.py new file mode 100644 index 0000000..fbd035e --- /dev/null +++ b/make_graph.py @@ -0,0 +1,37 @@ +# 生成云图 + + +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() \ No newline at end of file