import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# The code below fetches the data from the "Our World in Data" Github page.
# This is the data behind this graph/table: https://ourworldindata.org/grapher/cumulative-co-emissions
# If the url does not work you can also use the data provided, by uncommenting the line below.
df = pd.read_csv('owid-co2-data.csv', header =0)
# df = pd.read_csv('https://nyc3.digitaloceanspaces.com/owid-public/data/co2/owid-co2-data.csv', header=0)
# Select only data for World
df = df[df['country'] == 'World']
# Select year and Cumulative CO2
df = df[['year','cumulative_co2']]
# Turn year to integer
df['year'] = df['year'].astype(int)
# Subtract from every cumulative value what has been emitted until 1750
df['cumulative_co2']=df['cumulative_co2']-df['cumulative_co2'].iloc[0]
# For every row:
# 1. Calculate difference between "cumulative CO2 in year i" and "cumulative CO2 in last year"
# 2. Divide this difference with "cumulative CO2 in last year"
sums = []
for i in range(len(df)):
share_gen = (df['cumulative_co2'].iloc[-1]-df['cumulative_co2'].iloc[i])/df['cumulative_co2'].iloc[-1]*100
sums.append(share_gen)
# Add the new column of sums to the original DataFrame
df['share_gen'] = sums
# Select every fifth row
df = df.iloc[::5]
# Select data for people who were born in the last 100 years
scope = int(100/5)
df = df.tail(scope).reset_index(drop=True)
min_year = df['year'].iloc[0].item()
max_year = df['year'].iloc[-1].item()
# Create a Dash application
app = dash.Dash(__name__)
app.layout = html.Div([
html.P('1. Works best on desktop 2. Use the slider to adjust the graph 3. Hoover mouse over graph to see (i.a.) the download button', style={'textAlign': 'left'}),
dcc.Slider(
id='year-slider',
min=min_year,
max=max_year,
value=1985,
marks={str(year): str(year) for year in range(min_year,max_year+1, 5)},
step=None
),
dcc.Graph(
id="sine-graph",
style={'height': '90vh', 'width': '100vw'}
),
])
@app.callback(
Output("sine-graph", "figure"),
[Input("year-slider", "value")]
)
def update_graph(year_born):
# Round the year born to intervals of 5
year_born_round = int(5 * round(float(year_born)/5))
# Find index in df of year born
year_born_index = df[df['year'] == year_born_round].index.tolist()[0]
# data frame to plotly graph
fig = go.Figure(data=[
go.Bar(y=df['year'], x=df['share_gen'], orientation='h',
showlegend=False,
marker_color=['silver' if i != year_born_index else 'lightcoral' for i in range(df.shape[0])],
name = '')
])
subtitle= "See: https://git.pub.solar/misha/CO2-generation (original from https://x.com/neilrkaye/status/1365247133507604486)"
explainer = "If you were born in %s, %s%% of
the total amount of CO2 that has
been emitted since 1750, has been
emitted in your lifetime." % (int(df['year'].iloc[year_born_index].item()), int(df['share_gen'].iloc[year_born_index].item()))
(int(df['year'].iloc[year_born_index].item()), int(df['share_gen'].iloc[year_born_index].item()))
# Create subplot for vertical line
fig_vline = go.Figure(data=[go.Scatter(x = [df['share_gen'].iloc[year_born_index]]*2,
y=[df['year'].min()-2.5, df['year'].iloc[year_born_index]], mode='lines',
showlegend=False,
line=dict(color='lightcoral'))])
# Merge figures
fig.add_trace(fig_vline.data[0])
fig.update_layout(title_text='Percentage of global CO2 emissions since 1750 occurring in your lifetime (last updated: %s)
%s' % (df['year'].iloc[-1],subtitle),
xaxis_title="percentage", yaxis_title="year of birth")
fig.update_yaxes(range=[df['year'].min()-2.5, df['year'].max()+2.5],
tickmode = 'array',
tickvals = list(range(df['year'].min(), df['year'].max(), 5)))
fig.add_annotation(
text=explainer,
xref="paper", yref="paper",
x=0.99, y=0.98, showarrow=False,
align="left",
font=dict(size=16),
bgcolor="lightcoral",
borderwidth=2
)
return fig
if __name__ == '__main__':
app.run_server(debug=True,host='0.0.0.0')