Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import requests
|
4 |
import plotly.express as px
|
|
|
5 |
import io
|
6 |
|
7 |
# 設置顏色主題
|
@@ -30,6 +31,17 @@ def beautify_chart(fig):
|
|
30 |
fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
31 |
return fig
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# **生成 Plotly 圖表**
|
34 |
def generate_plots(df, df_name, selected_columns, chart_type):
|
35 |
selected_columns = list(selected_columns) # 確保是列表
|
@@ -51,8 +63,6 @@ def generate_plots(df, df_name, selected_columns, chart_type):
|
|
51 |
|
52 |
if chart_type == "折線圖":
|
53 |
fig = px.line(df, x="公司名稱", y=valid_selected_columns, title=f"{df_name} - 折線圖", color_discrete_sequence=theme)
|
54 |
-
elif chart_type == "散點圖":
|
55 |
-
fig = px.scatter(df, x="公司名稱", y=valid_selected_columns, title=f"{df_name} - 散點圖", color_discrete_sequence=theme)
|
56 |
elif chart_type == "長條圖":
|
57 |
fig = px.bar(df, x="公司名稱", y=valid_selected_columns, title=f"{df_name} - 長條圖", color_discrete_sequence=theme)
|
58 |
elif chart_type == "餅圖":
|
@@ -60,10 +70,14 @@ def generate_plots(df, df_name, selected_columns, chart_type):
|
|
60 |
total_emissions = total_emissions.melt(id_vars=["公司名稱"], value_vars=valid_selected_columns, var_name="排放類型", value_name="總排放量")
|
61 |
fig = px.pie(total_emissions, values='總排放量', names='公司名稱', title=f"{df_name} - 餅圖", color_discrete_sequence=theme, hole=0.3)
|
62 |
fig.update_traces(textposition='inside', textinfo='percent+label')
|
63 |
-
|
64 |
if fig:
|
65 |
fig = beautify_chart(fig)
|
66 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# **下載數據**
|
69 |
urls = [
|
@@ -86,7 +100,7 @@ emission_columns = [col for col in ["範疇一排放量(噸CO2e)", "範疇二排
|
|
86 |
selected_columns = st.multiselect("選擇要顯示的排放類別", emission_columns, default=emission_columns[:1] if emission_columns else [])
|
87 |
|
88 |
# **選擇圖表類型**
|
89 |
-
chart_type = st.selectbox("選擇圖表類型", ["折線圖", "
|
90 |
|
91 |
# **生成圖表**
|
92 |
if selected_columns:
|
|
|
2 |
import pandas as pd
|
3 |
import requests
|
4 |
import plotly.express as px
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
import io
|
7 |
|
8 |
# 設置顏色主題
|
|
|
31 |
fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
32 |
return fig
|
33 |
|
34 |
+
# **生成 Matplotlib 圖表**
|
35 |
+
def generate_matplotlib_plot(df, selected_columns):
|
36 |
+
fig, ax = plt.subplots()
|
37 |
+
for col in selected_columns:
|
38 |
+
ax.plot(df["公司名稱"], df[col], label=col)
|
39 |
+
ax.set_xlabel("公司名稱")
|
40 |
+
ax.set_ylabel("排放量")
|
41 |
+
ax.set_title("Matplotlib 折線圖")
|
42 |
+
ax.legend()
|
43 |
+
st.pyplot(fig)
|
44 |
+
|
45 |
# **生成 Plotly 圖表**
|
46 |
def generate_plots(df, df_name, selected_columns, chart_type):
|
47 |
selected_columns = list(selected_columns) # 確保是列表
|
|
|
63 |
|
64 |
if chart_type == "折線圖":
|
65 |
fig = px.line(df, x="公司名稱", y=valid_selected_columns, title=f"{df_name} - 折線圖", color_discrete_sequence=theme)
|
|
|
|
|
66 |
elif chart_type == "長條圖":
|
67 |
fig = px.bar(df, x="公司名稱", y=valid_selected_columns, title=f"{df_name} - 長條圖", color_discrete_sequence=theme)
|
68 |
elif chart_type == "餅圖":
|
|
|
70 |
total_emissions = total_emissions.melt(id_vars=["公司名稱"], value_vars=valid_selected_columns, var_name="排放類型", value_name="總排放量")
|
71 |
fig = px.pie(total_emissions, values='總排放量', names='公司名稱', title=f"{df_name} - 餅圖", color_discrete_sequence=theme, hole=0.3)
|
72 |
fig.update_traces(textposition='inside', textinfo='percent+label')
|
73 |
+
|
74 |
if fig:
|
75 |
fig = beautify_chart(fig)
|
76 |
st.plotly_chart(fig, use_container_width=True)
|
77 |
+
|
78 |
+
# 額外顯示 Matplotlib 圖表
|
79 |
+
st.subheader("📉 Matplotlib 折線圖")
|
80 |
+
generate_matplotlib_plot(df, valid_selected_columns)
|
81 |
|
82 |
# **下載數據**
|
83 |
urls = [
|
|
|
100 |
selected_columns = st.multiselect("選擇要顯示的排放類別", emission_columns, default=emission_columns[:1] if emission_columns else [])
|
101 |
|
102 |
# **選擇圖表類型**
|
103 |
+
chart_type = st.selectbox("選擇圖表類型", ["折線圖", "長條圖", "餅圖"])
|
104 |
|
105 |
# **生成圖表**
|
106 |
if selected_columns:
|