From f57bf172a144e5bd87d3ad91ec0a221f1f897eff Mon Sep 17 00:00:00 2001 From: pioc37juv <1245880206@qq.com> Date: Wed, 18 Sep 2024 19:50:12 +0800 Subject: [PATCH] =?UTF-8?q?Delete=20'=E8=AF=8D=E4=BA=91=E5=9B=BE.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 词云图.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 词云图.py diff --git a/词云图.py b/词云图.py deleted file mode 100644 index edd2ecf..0000000 --- a/词云图.py +++ /dev/null @@ -1,40 +0,0 @@ -import os - -import matplotlib.pyplot as plt -import pandas as pd -from wordcloud import WordCloud - - -def generate_wordcloud_from_excel(file_path): - - # 1、读取相应文件 - df = pd.read_excel(file_path) - - # 2、确认是否包含'词汇'和'频率'两列 - if '词汇' not in df.columns or '频率' not in df.columns: - print("excel文件中缺少 '词汇' 或 '频率' 列。") - return - - # 3、将词汇和频率转换为字典 - content = dict(zip(df['词汇'], df['频率'])) - - # 4、生成词云图 - wc = WordCloud(font_path='simhei.ttf', background_color='white', width=1371, height=771) # 生成参数 - wc.generate_from_frequencies(content) # WordCloud库中的generate_from_frequencies方法用于根据提供的词频数据生成词云。‌这个方法需要一个字典作为输入,其中键是单词,值是对应的词频。 - - # 5、保存词云图到指定位置 - output_folder = 'output' # 指定输出文件夹 - if not os.path.exists(output_folder): # 如果文件夹不存在就创建它 - os.makedirs(output_folder) - output_path = os.path.join(output_folder, '词云图.png') # 生成完整的文件路径 - wc.to_file(output_path) - - # 6、显示词云图 - plt.imshow(wc, interpolation='bilinear') - plt.axis('off') # 不显示坐标轴 - plt.show() - - -if __name__ == '__main__': - file_path = 'output\\高频词.xlsx' # 可替换为任意excel文件路径 - generate_wordcloud_from_excel(file_path)