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.
35 lines
1.2 KiB
35 lines
1.2 KiB
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
import matplotlib.pyplot as plt
|
|
from wordcloud import WordCloud
|
|
|
|
class DataVisualizer:
|
|
"""数据可视化类"""
|
|
|
|
def __init__(self):
|
|
print("可视化器初始化")
|
|
plt.rcParams['font.sans-serif'] = ['SimHei'] # 支持中文显示
|
|
plt.rcParams['axes.unicode_minus'] = False
|
|
|
|
def generate_wordcloud(self, word_freq, save_path='wordcloud.png'):
|
|
"""生成词云图"""
|
|
print("生成词云图中...")
|
|
wc = WordCloud(
|
|
width=800,
|
|
height=600,
|
|
background_color='white',
|
|
font_path='SimHei.ttf'
|
|
).generate_from_frequencies(word_freq)
|
|
|
|
plt.figure(figsize=(10, 8))
|
|
plt.imshow(wc, interpolation='bilinear')
|
|
plt.axis('off')
|
|
plt.title('大语言模型应用弹幕词云')
|
|
plt.savefig(save_path, dpi=300, bbox_inches='tight')
|
|
plt.show()
|
|
print(f"词云图已保存至: {save_path}")
|
|
|
|
if __name__ == "__main__":
|
|
visualizer = DataVisualizer()
|
|
test_freq = {'大语言模型': 50, 'AI应用': 30, '智能助手': 20, '机器学习': 15}
|
|
visualizer.generate_wordcloud(test_freq) |