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.
AAA/view_visualizer.py

44 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import matplotlib.pyplot as plt
# 设置matplotlib支持中文显示
plt.rcParams["font.family"] = ["SimHei"]
class ViewDistributionVisualizer:
@staticmethod
def plot(view_counts, filename="主流观点分布.png"):
"""生成主流观点分布柱状图并保存"""
# 创建画布设置大小为10x6
plt.figure(figsize=(10, 6))
# 定义柱状图颜色列表
colors = ['#4CAF50', '#2196F3', '#f44336', '#FFC107', "#5E06EB"]
# 绘制柱状图x轴为观点类别y轴为对应的弹幕数量
bars = plt.bar(view_counts.keys(), view_counts.values(), color=colors)
# 设置图表标题
plt.title("用户对大语言模型的主要关注点分布")
# 设置y轴标签
plt.ylabel("弹幕数量")
# x轴标签旋转30度避免文字重叠
plt.xticks(rotation=30)
# 为每个柱子添加数值标签
for bar in bars:
height = bar.get_height() # 获取柱子高度(即弹幕数量)
# 在柱子顶部居中位置添加数值标签
plt.text(bar.get_x() + bar.get_width()/2, height + 0, f"{height}", ha="center")
# 自动调整布局,避免元素重叠
plt.tight_layout()
# 保存图表到文件设置dpi为300以保证清晰度
plt.savefig(filename, dpi=300)
# 显示图表
plt.show()
print(f"观点分布图保存: {filename}")
@staticmethod
def print_stats(view_counts):
"""在控制台打印各观点类别的统计结果"""
print("\n===== 主流观点统计 =====")
# 遍历观点类别及其对应的弹幕数量并打印
for view, count in view_counts.items():
print(f"{view}: {count}")