@@ -23,10 +23,65 @@ def load_data():
23
23
'nuclear_electricity' : "Nuclear"
24
24
}, inplace = True )
25
25
26
- df .drop (columns = [])
26
+ df = df [~ df .country .isin ([
27
+ "G20 (Ember)" ,
28
+ "OECD (Ember)" ,
29
+ "Asia (Ember)" ,
30
+ "G7 (Ember)" ,
31
+ "OECD (EI)" ,
32
+ "Asia Pacific (EI)" ,
33
+ "High-income countries" ,
34
+ "Europe (Ember)" ,
35
+ "Non-OECD (EI)" ,
36
+ "Asia" ,
37
+ "North America (Ember)" ,
38
+ "Upper-middle-income countries" ,
39
+ "Europe (EI)" ,
40
+ "Oceania (Ember)" ,
41
+ "ASEAN (Ember)" ,
42
+ "Lower-middle-income countries" ,
43
+ "European Union (27)" ,
44
+ "North America (EI)"
45
+ ])]
27
46
28
47
return df .sort_values ('year' )
29
48
49
+
50
+ @st .cache_data
51
+ def load_world_data (map_data ):
52
+ world = gpd .read_file ("ne_110m_admin_0_countries/ne_110m_admin_0_countries.dbf" )
53
+ # data processing
54
+ world .rename (columns = {"ADMIN" : "country" }, inplace = True )
55
+ # replace country name to fit the other dataset
56
+ world .country = world .country .replace (
57
+ ["United States of America" , "Democratic Republic of the Congo" , "Republic of the Congo" , "United Republic of Tanzania" , "The Bahamas" , "Czechia" , "eSwatini" , "Republic of Serbia" ],
58
+ ["United States" , "Democratic Republic of Congo" , "Congo" , "Tanzania" , "Bahamas" , "Czechoslovakia" , "Eswatini" , "Serbia" ]
59
+ )
60
+
61
+ # world.SOV_A3 = world.SOV_A3.replace(["US1", "CH1", "FR1", "KA1", "GB1", "NZ1", "AU1"], ["USA", "CHN", "FRA", "KAZ", "GBR", "NZL", "AUS"])
62
+ # latest_year = recent_data['year'].max() - pd.Timedelta(days=365*2)
63
+ # latest_data = recent_data[recent_data['year'] == latest_year]
64
+ world = world .merge (map_data , on = ['country' ])
65
+ return world
66
+
67
+
68
+ @st .cache_data
69
+ def get_map_plot (energy_type_map , year_map ):
70
+ match energy_type_map :
71
+ case "Solar" :
72
+ color_scale = "Oranges"
73
+ case "Wind" :
74
+ color_scale = "Greens"
75
+ case "Hydro" :
76
+ color_scale = "Blues"
77
+ case _ :
78
+ color_scale = "Viridis"
79
+
80
+ fig = px .choropleth (world , locations = 'ADM0_A3' , color = energy_type_map ,
81
+ hover_name = 'country' , projection = 'natural earth2' , color_continuous_scale = color_scale ,
82
+ title = f'{ energy_type_map .replace ("_" , " " ).title ()} Production in { year_map } ' )
83
+
84
+ return fig
30
85
df = load_data ()
31
86
32
87
@@ -41,7 +96,7 @@ def load_data():
41
96
42
97
# Filter data based on selected years
43
98
filtered_df = df [(df ['year' ].dt .year >= start_year ) & (df ['year' ].dt .year <= end_year )]
44
- # st.write(filtered_df)
99
+
45
100
# Global trend plot
46
101
st .header ('Global Energy Production Trends' )
47
102
energy_types = st .multiselect (
@@ -60,34 +115,23 @@ def load_data():
60
115
# Map of energy production
61
116
st .header ('Energy Production Map' )
62
117
energy_type_map = st .selectbox ('Select Energy Type for Map' , energy_types )
63
- year_map = st .slider ('Select Year for Map' , start_year , end_year , end_year )
118
+ year_map = st .slider ('Select Year for Map' , start_year , end_year , end_year - 10 )
64
119
65
- map_data = filtered_df [filtered_df ['year' ].dt .year == year_map ]
66
- world = gpd .read_file ("ne_110m_admin_0_countries/ne_110m_admin_0_countries.dbf" )
67
- # data processing
68
- world .rename (columns = {"ADMIN" : "country" }, inplace = True )
69
- # replace country name to fit the other dataset
70
- world .country = world .country .replace (
71
- ["United States of America" , "Democratic Republic of the Congo" , "Republic of the Congo" , "United Republic of Tanzania" , "The Bahamas" , "Czechia" , "eSwatini" , "Republic of Serbia" ],
72
- ["United States" , "Democratic Republic of Congo" , "Congo" , "Tanzania" , "Bahamas" , "Czechoslovakia" , "Eswatini" , "Serbia" ]
73
- )
74
120
75
- # world.SOV_A3 = world.SOV_A3.replace(["US1", "CH1", "FR1", "KA1", "GB1", "NZ1", "AU1"], ["USA", "CHN", "FRA", "KAZ", "GBR", "NZL", "AUS"])
76
- # latest_year = recent_data['year'].max() - pd.Timedelta(days=365*2)
77
- # latest_data = recent_data[recent_data['year'] == latest_year]
78
- world = world .merge (map_data , on = ['country' ])
121
+ map_data = filtered_df [filtered_df ['year' ].dt .year == year_map ]
122
+ world = load_world_data (map_data )
79
123
80
- fig = px .choropleth (world , locations = 'ADM0_A3' , color = energy_type_map ,
81
- hover_name = 'country' , projection = 'natural earth2' , color_continuous_scale = 'Viridis' ,
82
- title = f'{ energy_type_map .replace ("_" , " " ).title ()} Production in { year_map } ' )
83
- # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
124
+ fig = get_map_plot (energy_type_map , year_map )
125
+ fig .update_layout (margin = {"r" :0 ,"t" :0 ,"l" :0 ,"b" :0 })
84
126
st .plotly_chart (fig )
85
127
86
128
# Top countries comparison
87
129
st .header ('Top Countries Comparison' )
88
- n_countries = st .slider ('Number of top countries to compare' , 1 , 10 , 5 )
130
+ n_countries = st .slider ('Number of top countries to compare' , 1 , 20 , 15 )
89
131
top_countries = filtered_df .groupby ('country' )[energy_types [0 ]].mean ().nlargest (n_countries ).index .tolist ()
90
132
133
+ st .write (top_countries )
134
+
91
135
top_countries_data = filtered_df [filtered_df ['country' ].isin (top_countries )]
92
136
93
137
fig = px .line (top_countries_data , x = 'year' , y = energy_types [0 ], color = 'country' ,
@@ -96,21 +140,23 @@ def load_data():
96
140
97
141
# Energy mix comparison
98
142
st .header ('Energy Mix Comparison' )
99
- selected_country = st .selectbox ('Select a Country' , df ['country' ].unique (), index = None ,
100
- placeholder = "Select a country ..." ,)
143
+ selected_country = st .selectbox ('Select a Country' , df ['country' ].unique (), index = None )
144
+ if selected_country == None :
145
+ selected_country = "Italy"
146
+
101
147
country_data = filtered_df [filtered_df ['country' ] == selected_country ]
102
- st .write (country_data )
148
+ # st.write(country_data)
103
149
104
150
105
151
energy_mix = country_data [energy_types ]
106
- # energy_mix = energy_mix.div(energy_mix.sum(axis=1), axis=0) * 100
107
152
energy_mix ['year' ] = country_data .year
108
153
109
154
fig = px .area (energy_mix .dropna (), x = 'year' , y = energy_types ,
110
155
title = f'Energy Mix for { selected_country } ' )
111
156
fig .update_yaxes (title = "Electricity Production (TWh)" )
112
157
st .plotly_chart (fig )
113
- st .write (energy_mix )
158
+ # st.write(energy_mix)
159
+
114
160
115
161
st .write ("""
116
162
This Streamlit app provides an interactive analysis of global energy production trends.
0 commit comments