Skip to content

Commit f4e3876

Browse files
committed
added streamlit app for renawables energies
1 parent a688282 commit f4e3876

14 files changed

+2181
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ Moreover, I will use this website as a blogging platform. I will talk about seve
3434
- [ ] Music Generation
3535
- [ ] Text-to-Speech
3636
- Graph NN
37-
- Reinforcement Learing:
37+
- Reinforcement Learing:
38+
4. Renewables:
39+
- [ ] World Energy Production
40+
- [ ] World Energy Potential
41+
- [ ] Solar Panel Simulations

content/posts/renewables/world-energy/analysis.ipynb

Lines changed: 1508 additions & 0 deletions
Large diffs are not rendered by default.
Loading
Loading
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import streamlit as st
2+
import pandas as pd
3+
import matplotlib.pyplot as plt
4+
import seaborn as sns
5+
import geopandas as gpd
6+
import plotly.express as px
7+
8+
energy_columns = ['renewables_electricity', 'coal_electricity', 'gas_electricity', 'oil_electricity', 'nuclear_electricity', 'wind_electricity', 'solar_electricity', "hydro_electricity"]
9+
# Load and prepare data
10+
@st.cache_data
11+
def load_data():
12+
url = "https://raw.githubusercontent.com/owid/energy-data/master/owid-energy-data.csv"
13+
df = pd.read_csv(url)
14+
df['year'] = pd.to_datetime(df['year'], format='%Y')
15+
df.rename(columns={
16+
"renewables_electricity": "Renewables",
17+
"hydro_electricity": "Hydro",
18+
"solar_electricity": "Solar",
19+
"wind_electricity": "Wind",
20+
"coal_electricity": 'Coal',
21+
'gas_electricity': "Gas",
22+
'oil_electricity': "Oil",
23+
'nuclear_electricity' : "Nuclear"
24+
}, inplace=True)
25+
26+
df.drop(columns=[])
27+
28+
return df.sort_values('year')
29+
30+
df = load_data()
31+
32+
33+
# Streamlit app
34+
st.title('Global Energy Production Analysis')
35+
# st.write(df.columns)
36+
# st.write([col.split("_")[0].capitalize() for col in energy_columns])
37+
# Sidebar for user input
38+
st.sidebar.header('Filter Data')
39+
start_year = st.sidebar.slider('Start Year', 1900, 2023, 1980)
40+
end_year = st.sidebar.slider('End Year', 1900, 2022, 2023)
41+
42+
# Filter data based on selected years
43+
filtered_df = df[(df['year'].dt.year >= start_year) & (df['year'].dt.year <= end_year)]
44+
# st.write(filtered_df)
45+
# Global trend plot
46+
st.header('Global Energy Production Trends')
47+
energy_types = st.multiselect(
48+
'Select Energy Types',
49+
[col.split("_")[0].capitalize() for col in energy_columns],
50+
default=["Solar", "Wind", "Hydro"]
51+
)
52+
53+
global_trend = filtered_df.groupby('year')[energy_types].mean()
54+
55+
fig = px.line(global_trend.reset_index(), x='year', y=energy_types,
56+
title='Global Energy Production Trends')
57+
fig.update_yaxes(title="Electricity Production (TWh)")
58+
st.plotly_chart(fig)
59+
60+
# Map of energy production
61+
st.header('Energy Production Map')
62+
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)
64+
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+
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'])
79+
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})
84+
st.plotly_chart(fig)
85+
86+
# Top countries comparison
87+
st.header('Top Countries Comparison')
88+
n_countries = st.slider('Number of top countries to compare', 1, 10, 5)
89+
top_countries = filtered_df.groupby('country')[energy_types[0]].mean().nlargest(n_countries).index.tolist()
90+
91+
top_countries_data = filtered_df[filtered_df['country'].isin(top_countries)]
92+
93+
fig = px.line(top_countries_data, x='year', y=energy_types[0], color='country',
94+
title=f'Top {n_countries} Countries in {energy_types[0].replace("_", " ").title()} Production')
95+
st.plotly_chart(fig)
96+
97+
# Energy mix comparison
98+
st.header('Energy Mix Comparison')
99+
selected_country = st.selectbox('Select a Country', df['country'].unique(), index=None,
100+
placeholder="Select a country ...",)
101+
country_data = filtered_df[filtered_df['country'] == selected_country]
102+
st.write(country_data)
103+
104+
105+
energy_mix = country_data[energy_types]
106+
# energy_mix = energy_mix.div(energy_mix.sum(axis=1), axis=0) * 100
107+
energy_mix['year'] = country_data.year
108+
109+
fig = px.area(energy_mix.dropna(), x='year', y=energy_types,
110+
title=f'Energy Mix for {selected_country}')
111+
fig.update_yaxes(title="Electricity Production (TWh)")
112+
st.plotly_chart(fig)
113+
st.write(energy_mix)
114+
115+
st.write("""
116+
This Streamlit app provides an interactive analysis of global energy production trends.
117+
You can use the sidebar and various selectors to customize the visualizations and explore different aspects of energy production data.
118+
""")
Binary file not shown.

0 commit comments

Comments
 (0)