|
|
import pandas as pd
|
|
|
import numpy as np
|
|
|
from datetime import datetime, timedelta
|
|
|
import matplotlib.pyplot as plt
|
|
|
from snownlp import SnowNLP
|
|
|
from matplotlib.font_manager import FontProperties
|
|
|
|
|
|
def plot_sentiment_distribution(danmaku_list):
|
|
|
# 计算每条弹幕的情感得分
|
|
|
sentiments = [SnowNLP(dmk).sentiments for dmk in danmaku_list]
|
|
|
|
|
|
# 绘制情感得分直方图
|
|
|
plt.figure(figsize=(10, 6))
|
|
|
plt.hist(sentiments, bins=20, color='lightgreen', edgecolor='black')
|
|
|
plt.xlabel('Sentiment Score')
|
|
|
plt.ylabel('Bullet Screen Count" or "Danmaku Count')
|
|
|
plt.title('Danmaku Sentiment Score Distribution Chart')
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
def plot_top_danmakus(danmaku_frequency):
|
|
|
# 提取词语和对应的频率
|
|
|
words = list(danmaku_frequency.keys())
|
|
|
frequencies = list(danmaku_frequency.values())
|
|
|
|
|
|
# 绘制柱状图
|
|
|
plt.figure(figsize=(10, 6))
|
|
|
plt.bar(words, frequencies, color='skyblue')
|
|
|
plt.xlabel('Danmaku Words')
|
|
|
plt.ylabel('Frequency of Appearance')
|
|
|
plt.title('High-Frequency Danmaku Words Statistics')
|
|
|
plt.xticks(rotation=45)
|
|
|
plt.tight_layout()
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
def create_manual_data():
|
|
|
# 日期列表
|
|
|
date_list = pd.date_range(start='2023-07-26', end='2023-08-11', freq='D')
|
|
|
|
|
|
# 手动指定情感得分,先上升后下降,8月4日达到最高点
|
|
|
sentiment_values = [
|
|
|
0.5, # 7月26日
|
|
|
0.35, # 7月27日
|
|
|
0.4, # 7月28日
|
|
|
0.45, # 7月29日
|
|
|
0.5, # 7月30日
|
|
|
0.55, # 7月31日
|
|
|
0.6, # 8月1日
|
|
|
0.7, # 8月2日
|
|
|
0.8, # 8月3日
|
|
|
0.9, # 8月4日(峰值)
|
|
|
0.8, # 8月5日
|
|
|
0.7, # 8月6日
|
|
|
0.6, # 8月7日
|
|
|
0.5, # 8月8日
|
|
|
0.4, # 8月9日
|
|
|
0.35, # 8月10日
|
|
|
0.3 # 8月11日
|
|
|
]
|
|
|
|
|
|
# 创建 DataFrame
|
|
|
data = {'date': date_list, 'sentiment': sentiment_values}
|
|
|
df = pd.DataFrame(data)
|
|
|
return df
|
|
|
|
|
|
|
|
|
def plot_sentiment_combined(sentiment_df):
|
|
|
# 设置字体为微软雅黑
|
|
|
font = FontProperties(fname=r'C:\Windows\Fonts\msyh.ttc', size=12)
|
|
|
|
|
|
# 设置图形大小
|
|
|
plt.figure(figsize=(12, 6))
|
|
|
|
|
|
# 绘制柱状图
|
|
|
plt.bar(sentiment_df['date'], sentiment_df['sentiment'], color='skyblue', label='情感得分(柱状图)')
|
|
|
|
|
|
# 绘制折线图,使用相同的Y轴
|
|
|
plt.plot(sentiment_df['date'], sentiment_df['sentiment'], color='red', marker='o', label='情感得分(折线图)')
|
|
|
|
|
|
# 设置标题和标签
|
|
|
plt.title('情感得分随时间的变化', fontproperties=font)
|
|
|
plt.xlabel('日期', fontproperties=font)
|
|
|
plt.ylabel('情感得分', fontproperties=font)
|
|
|
|
|
|
# 设置日期格式和字体
|
|
|
plt.xticks(sentiment_df['date'], sentiment_df['date'].dt.strftime('%m-%d'), rotation=45, fontproperties=font)
|
|
|
plt.yticks(fontproperties=font)
|
|
|
|
|
|
# 添加数据标签
|
|
|
for x, y in zip(sentiment_df['date'], sentiment_df['sentiment']):
|
|
|
plt.text(x, y + 0.02, f'{y:.2f}', ha='center', fontproperties=font)
|
|
|
|
|
|
# 添加网格线
|
|
|
plt.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
|
|
# 显示图例
|
|
|
plt.legend(prop=font)
|
|
|
|
|
|
# 调整布局以防止标签重叠
|
|
|
plt.tight_layout()
|
|
|
|
|
|
# 显示图形
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
# 使用示例
|
|
|
sentiment_df = create_manual_data()
|
|
|
plot_sentiment_combined(sentiment_df)
|