shiue2000 commited on
Commit
9925690
·
verified ·
1 Parent(s): dc93dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py CHANGED
@@ -62,12 +62,18 @@ def run_analysis():
62
 
63
  # ----------------- 圖表 -----------------
64
  # 1. 當日情緒比例
 
 
 
65
  fig1 = plt.figure(figsize=(8,5))
66
  summary[['正面比率','負面比率']].plot(kind='bar', stacked=True, colormap='coolwarm', ax=fig1.gca())
67
  fig1.gca().set_title("候選人當日社群情緒比例")
68
  img_b64_today = plot_to_base64(fig1)
69
 
70
  # 2. 歷史情緒趨勢
 
 
 
71
  fig2 = plt.figure(figsize=(10,5))
72
  for c in candidates:
73
  temp = df_hist[df_hist['候選人']==c]
@@ -81,6 +87,9 @@ def run_analysis():
81
 
82
  # 3~8 其他圖表生成
83
  # 社群情感趨勢
 
 
 
84
  fig3 = plt.figure(figsize=(8,5))
85
  plt.plot(range(7), [random.random() for _ in range(7)], marker='o', label="正面")
86
  plt.plot(range(7), [random.random() for _ in range(7)], marker='x', label="負面")
@@ -89,6 +98,9 @@ def run_analysis():
89
  img_social_sentiment = plot_to_base64(fig3)
90
 
91
  # 各平台表現
 
 
 
92
  fig4 = plt.figure(figsize=(8,5))
93
  platforms=["X","Facebook","Instagram","PTT","Line"]
94
  plt.bar(platforms, [random.randint(10,100) for _ in platforms], color='skyblue')
@@ -96,6 +108,9 @@ def run_analysis():
96
  img_platform_performance = plot_to_base64(fig4)
97
 
98
  # 候選人社群量趨勢
 
 
 
99
  fig5 = plt.figure(figsize=(8,5))
100
  for c in candidates: plt.plot(range(7), [random.randint(5,20) for _ in range(7)], marker='o', label=c)
101
  plt.title("候選人社群量趨勢")
@@ -103,12 +118,18 @@ def run_analysis():
103
  img_candidate_volume = plot_to_base64(fig5)
104
 
105
  # 候選人社群量分析
 
 
 
106
  fig6 = plt.figure(figsize=(8,5))
107
  summary[['正面比率','負面比率']].plot(kind='bar', stacked=True, colormap='coolwarm', ax=fig6.gca())
108
  fig6.gca().set_title("候選人社群量分析(正/負面情緒)")
109
  img_candidate_sentiment = plot_to_base64(fig6)
110
 
111
  # 知識圖譜
 
 
 
112
  fig7, ax7 = plt.subplots(figsize=(8,6))
113
  G=nx.Graph()
114
  for c in candidates: G.add_node(c)
@@ -141,6 +162,9 @@ def run_analysis():
141
  """
142
 
143
  # HTML template
 
 
 
144
  html_template = open("templates/index.html").read()
145
  html_content = html_template.format(
146
  report_date=datetime.now().strftime('%Y-%m-%d %H:%M'),
 
62
 
63
  # ----------------- 圖表 -----------------
64
  # 1. 當日情緒比例
65
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
66
+ plt.rcParams['axes.unicode_minus'] = False
67
+
68
  fig1 = plt.figure(figsize=(8,5))
69
  summary[['正面比率','負面比率']].plot(kind='bar', stacked=True, colormap='coolwarm', ax=fig1.gca())
70
  fig1.gca().set_title("候選人當日社群情緒比例")
71
  img_b64_today = plot_to_base64(fig1)
72
 
73
  # 2. 歷史情緒趨勢
74
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
75
+ plt.rcParams['axes.unicode_minus'] = False
76
+
77
  fig2 = plt.figure(figsize=(10,5))
78
  for c in candidates:
79
  temp = df_hist[df_hist['候選人']==c]
 
87
 
88
  # 3~8 其他圖表生成
89
  # 社群情感趨勢
90
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
91
+ plt.rcParams['axes.unicode_minus'] = False
92
+
93
  fig3 = plt.figure(figsize=(8,5))
94
  plt.plot(range(7), [random.random() for _ in range(7)], marker='o', label="正面")
95
  plt.plot(range(7), [random.random() for _ in range(7)], marker='x', label="負面")
 
98
  img_social_sentiment = plot_to_base64(fig3)
99
 
100
  # 各平台表現
101
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
102
+ plt.rcParams['axes.unicode_minus'] = False
103
+
104
  fig4 = plt.figure(figsize=(8,5))
105
  platforms=["X","Facebook","Instagram","PTT","Line"]
106
  plt.bar(platforms, [random.randint(10,100) for _ in platforms], color='skyblue')
 
108
  img_platform_performance = plot_to_base64(fig4)
109
 
110
  # 候選人社群量趨勢
111
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
112
+ plt.rcParams['axes.unicode_minus'] = False
113
+
114
  fig5 = plt.figure(figsize=(8,5))
115
  for c in candidates: plt.plot(range(7), [random.randint(5,20) for _ in range(7)], marker='o', label=c)
116
  plt.title("候選人社群量趨勢")
 
118
  img_candidate_volume = plot_to_base64(fig5)
119
 
120
  # 候選人社群量分析
121
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
122
+ plt.rcParams['axes.unicode_minus'] = False
123
+
124
  fig6 = plt.figure(figsize=(8,5))
125
  summary[['正面比率','負面比率']].plot(kind='bar', stacked=True, colormap='coolwarm', ax=fig6.gca())
126
  fig6.gca().set_title("候選人社群量分析(正/負面情緒)")
127
  img_candidate_sentiment = plot_to_base64(fig6)
128
 
129
  # 知識圖譜
130
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
131
+ plt.rcParams['axes.unicode_minus'] = False
132
+
133
  fig7, ax7 = plt.subplots(figsize=(8,6))
134
  G=nx.Graph()
135
  for c in candidates: G.add_node(c)
 
162
  """
163
 
164
  # HTML template
165
+ plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'Arial Unicode MS', 'SimHei', 'DejaVu Sans']
166
+ plt.rcParams['axes.unicode_minus'] = False
167
+
168
  html_template = open("templates/index.html").read()
169
  html_content = html_template.format(
170
  report_date=datetime.now().strftime('%Y-%m-%d %H:%M'),