Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,6 +68,11 @@ def fig_to_base64():
|
|
| 68 |
def generate_charts(all_df, summary, df_hist):
|
| 69 |
results = {}
|
| 70 |
# 1. 每日情緒比例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
fig = plt.figure(figsize=(8, 5))
|
| 72 |
summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot(
|
| 73 |
kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca()
|
|
@@ -77,6 +82,11 @@ def generate_charts(all_df, summary, df_hist):
|
|
| 77 |
plt.xlabel("候選人")
|
| 78 |
results["img_b64_today"] = fig_to_base64()
|
| 79 |
# 2. 歷史情緒趨勢
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
fig = plt.figure(figsize=(10, 5))
|
| 81 |
for c in candidates:
|
| 82 |
temp = df_hist[df_hist['Candidate'] == c]
|
|
@@ -90,6 +100,11 @@ def generate_charts(all_df, summary, df_hist):
|
|
| 90 |
plt.legend()
|
| 91 |
results["img_b64_trend"] = fig_to_base64()
|
| 92 |
# 3. 社群情緒趨勢
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
sentiment_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Sentiment']).size().unstack(fill_value=0)
|
| 94 |
sentiment_trend = sentiment_trend.div(sentiment_trend.sum(axis=1), axis=0).fillna(0)
|
| 95 |
fig = plt.figure(figsize=(8, 5))
|
|
@@ -102,6 +117,11 @@ def generate_charts(all_df, summary, df_hist):
|
|
| 102 |
plt.legend()
|
| 103 |
results["img_social_sentiment"] = fig_to_base64()
|
| 104 |
# 4. 平台表現
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
platforms = ["X", "Facebook", "Instagram", "PTT", "Line"]
|
| 106 |
platform_counts = pd.Series({p: random.randint(10, 100) for p in platforms})
|
| 107 |
fig = plt.figure(figsize=(8, 5))
|
|
@@ -111,6 +131,11 @@ def generate_charts(all_df, summary, df_hist):
|
|
| 111 |
plt.ylabel("貼文數量")
|
| 112 |
results["img_platform_performance"] = fig_to_base64()
|
| 113 |
# 5. 候選人聲量趨勢
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
candidate_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Candidate']).size().unstack(fill_value=0)
|
| 115 |
fig = plt.figure(figsize=(8, 5))
|
| 116 |
for c in candidates:
|
|
@@ -122,6 +147,11 @@ def generate_charts(all_df, summary, df_hist):
|
|
| 122 |
plt.legend()
|
| 123 |
results["img_candidate_volume"] = fig_to_base64()
|
| 124 |
# 6. 候選人情緒分析
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
fig = plt.figure(figsize=(8, 5))
|
| 126 |
summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot(
|
| 127 |
kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca()
|
|
|
|
| 68 |
def generate_charts(all_df, summary, df_hist):
|
| 69 |
results = {}
|
| 70 |
# 1. 每日情緒比例
|
| 71 |
+
# ===== 字型與樣式 =====
|
| 72 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 73 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 74 |
+
plt.style.use("seaborn-v0_8")
|
| 75 |
+
|
| 76 |
fig = plt.figure(figsize=(8, 5))
|
| 77 |
summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot(
|
| 78 |
kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca()
|
|
|
|
| 82 |
plt.xlabel("候選人")
|
| 83 |
results["img_b64_today"] = fig_to_base64()
|
| 84 |
# 2. 歷史情緒趨勢
|
| 85 |
+
# ===== 字型與樣式 =====
|
| 86 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 87 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 88 |
+
plt.style.use("seaborn-v0_8")
|
| 89 |
+
|
| 90 |
fig = plt.figure(figsize=(10, 5))
|
| 91 |
for c in candidates:
|
| 92 |
temp = df_hist[df_hist['Candidate'] == c]
|
|
|
|
| 100 |
plt.legend()
|
| 101 |
results["img_b64_trend"] = fig_to_base64()
|
| 102 |
# 3. 社群情緒趨勢
|
| 103 |
+
# ===== 字型與樣式 =====
|
| 104 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 105 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 106 |
+
plt.style.use("seaborn-v0_8")
|
| 107 |
+
|
| 108 |
sentiment_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Sentiment']).size().unstack(fill_value=0)
|
| 109 |
sentiment_trend = sentiment_trend.div(sentiment_trend.sum(axis=1), axis=0).fillna(0)
|
| 110 |
fig = plt.figure(figsize=(8, 5))
|
|
|
|
| 117 |
plt.legend()
|
| 118 |
results["img_social_sentiment"] = fig_to_base64()
|
| 119 |
# 4. 平台表現
|
| 120 |
+
# ===== 字型與樣式 =====
|
| 121 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 122 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 123 |
+
plt.style.use("seaborn-v0_8")
|
| 124 |
+
|
| 125 |
platforms = ["X", "Facebook", "Instagram", "PTT", "Line"]
|
| 126 |
platform_counts = pd.Series({p: random.randint(10, 100) for p in platforms})
|
| 127 |
fig = plt.figure(figsize=(8, 5))
|
|
|
|
| 131 |
plt.ylabel("貼文數量")
|
| 132 |
results["img_platform_performance"] = fig_to_base64()
|
| 133 |
# 5. 候選人聲量趨勢
|
| 134 |
+
# ===== 字型與樣式 =====
|
| 135 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 136 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 137 |
+
plt.style.use("seaborn-v0_8")
|
| 138 |
+
|
| 139 |
candidate_trend = all_df.groupby([pd.Grouper(key='Date', freq='D'), 'Candidate']).size().unstack(fill_value=0)
|
| 140 |
fig = plt.figure(figsize=(8, 5))
|
| 141 |
for c in candidates:
|
|
|
|
| 147 |
plt.legend()
|
| 148 |
results["img_candidate_volume"] = fig_to_base64()
|
| 149 |
# 6. 候選人情緒分析
|
| 150 |
+
# ===== 字型與樣式 =====
|
| 151 |
+
plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Noto Sans TC', 'SimHei', 'Arial Unicode MS']
|
| 152 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 153 |
+
plt.style.use("seaborn-v0_8")
|
| 154 |
+
|
| 155 |
fig = plt.figure(figsize=(8, 5))
|
| 156 |
summary[['Positive Ratio', 'Negative Ratio', 'Neutral Ratio']].plot(
|
| 157 |
kind='bar', stacked=True, colormap='coolwarm', ax=fig.gca()
|