|
|
|
|
@ -0,0 +1,44 @@
|
|
|
|
|
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}条")
|