{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sustainability - A side-effect of globalization?\n",
"\n",
"Globalization promises prosperity, but this may impact the health of the natural environment. Economic activities across the globe have reduced earth's ability to provide necessary resources. [Global warming](https://en.wikipedia.org/wiki/Global_warming) is one of the major environmental concerns which has been largely influenced by the emission of greenhouse gases like carbon dioxide (CO2).\n",
"\n",
"Let us try to find the connection between globalization and CO2 emissions across the world. The [dataset](https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions) was obtained from [Our World in Data](https://ourworldindata.org/), and contains [per capita](https://en.wikipedia.org/wiki/Per_capita) CO2 emission by various countries since the early 1800s.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CO2 emissions contributions of an average person in around the world\n",
"\n",
"Let's check out how people in different countries contributed to the CO2 emissions. Run the following cells to see a preview of the data and an animated [choropleth map](https://en.wikipedia.org/wiki/Choropleth_map)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import Python libraries\n",
"import pandas as pd\n",
"import plotly_express as px\n",
"from ipywidgets import interact, fixed, widgets, Layout, Button, Box, fixed, HBox, VBox\n",
"import plotly.graph_objects as go\n",
"from plotly.subplots import make_subplots\n",
"from IPython.display import clear_output\n",
"\n",
"# Don't show warnings in output\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"print('Libraries successfully imported.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import the dataset\n",
"df = pd.read_csv('./Data/co-emissions-per-capita.csv')\n",
"\n",
"# Data clean up - keep rows pertaining to 1851-current and rename the columns\n",
"df = df[df['Year'] > 1850]\\\n",
" .rename(columns={'Code':'Country Code',\n",
" 'Per capita CO₂ emissions (tonnes per capita)':'CO₂ emissions
(tonnes per capita)'})\n",
"\n",
"# Display the top 5 rows\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot an animated choropleth map (Execution of this code cell will take a little while)\n",
"\n",
"fig = px.choropleth(df, # dataframe with required data \n",
" locations=\"Country Code\", # Column containing country codes\n",
" color=\"CO₂ emissions
(tonnes per capita)\", # Color of country should be based on per capita CO₂ emission\n",
" hover_name=\"Entity\", # Title to add to hover information\n",
" hover_data=[\"CO₂ emissions
(tonnes per capita)\"], # Data to add to hover information\n",
" color_continuous_scale=px.colors.sequential.Reds, # Set the colorscale type\n",
" animation_frame = \"Year\", # Values based on which animation frame will be prepared\n",
" range_color = [0,20], # Range of the colorbar\n",
" \n",
" # Title of the chart\n",
" title = 'Per capita CO₂ emissions for countries around the world
\\\n",
"Source: Our World in Data'\n",
" )\n",
"\n",
"fig.update_layout(geo=dict(showcountries=True))\n",
"# Show the figure\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click on the ▶ (play) button to see it change over time. Move your mouse around the map to see values for the per capita CO2 emission by different countries."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Questions\n",
"1. Before 1900, which country had the highest per capita emission? Why might that be?\n",
"2. According to the latest data, how would you rate Canada's per capita emission as compared to other countries with large populations?\n",
"3. Is there significant inequality in per capita emissions around the world? How does this relate to globalization? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Global CO2 emissions shares of countries around the world\n",
"\n",
"Now that we've seen the per capita CO2 emissions for different countries, let's see if the overall emission growth trend is the same. It would also be beneficial to compare the share of different countries for different years.\n",
"\n",
"Let's pull out the annual emission data from the same [source](https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions). Run the code blocks below to see pie charts showing comparison for the years you choose."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import the dataset and remove rows with missing values\n",
"df2 = pd.read_csv('./Data/annual-co2-emissions-per-country.csv').dropna()\n",
"\n",
"# Data clean up - keep rows pertaining to 1851-current and remove entities named 'World' and 'Micronesia'\n",
"df2 = df2[df2['Year'] > 1850][df2['Entity'] != 'World'][df2['Code'] != 'FSM']\n",
"\n",
"# Display top 5 rows\n",
"df2.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define a callback function for \"Show CO₂ Emissions\" button\n",
"def compare_annual_emissions(ev):\n",
" \n",
" clear_output(wait=True)\n",
" \n",
" # Define display order for the buttons and menus\n",
" display(Box(children = [year1_menu, year2_menu], layout = box_layout))\n",
" display(VBox(children = [show_button], layout = Layout(display= 'flex', flex_flow= 'column', align_items= 'center', width='100%', justify_content = 'center')))\n",
"\n",
" # Filter the rows for user-selected years and convert them to percentage\n",
" subset1 = df2[df2['Year'] == year1_menu.value]\n",
" subset1['Annual CO₂ emissions (tonnes)'] = (100 * subset1['Annual CO₂ emissions (tonnes)'] / subset1['Annual CO₂ emissions (tonnes)'].sum()).round(1)\n",
" subset1 = subset1.sort_values('Annual CO₂ emissions (tonnes)',ascending=False)[:5]\n",
" \n",
" subset2 = df2[df2['Year'] == year2_menu.value]\n",
" subset2['Annual CO₂ emissions (tonnes)'] = (100 * subset2['Annual CO₂ emissions (tonnes)'] / subset2['Annual CO₂ emissions (tonnes)'].sum()).round(1)\n",
" subset2 = subset2.sort_values('Annual CO₂ emissions (tonnes)',ascending=False)[:5]\n",
" \n",
" # Make subplots\n",
" fig = make_subplots(rows=1, cols=2, subplot_titles=['Year {}'.format(year1_menu.value), 'Year {}'.format(year2_menu.value)])\n",
" \n",
" # Add trace for each year's bar chart\n",
" fig.add_trace(go.Bar(name='Year {}'.format(year1_menu.value),\n",
" x=subset1['Entity'],\n",
" y=subset1['Annual CO₂ emissions (tonnes)'],\n",
" showlegend=False),1,1)\n",
" \n",
" # Add trace for each year's bar chart\n",
" fig.add_trace(go.Bar(name='Year {}'.format(year2_menu.value),\n",
" x=subset2['Entity'],\n",
" y=subset2['Annual CO₂ emissions (tonnes)'],\n",
" showlegend=False),1,2)\n",
" \n",
" # Update yaxis properties of subplots\n",
" fig.update_yaxes(title_text=\"Share in Global CO₂ Emissions (%)\", row=1, col=1, range=[0,max(subset1['Annual CO₂ emissions (tonnes)'].max(),subset2['Annual CO₂ emissions (tonnes)'].max())])\n",
" fig.update_yaxes(title_text=\"Share in Global CO₂ Emissions (%)\", row=1, col=2, range=[0,max(subset1['Annual CO₂ emissions (tonnes)'].max(),subset2['Annual CO₂ emissions (tonnes)'].max())])\n",
" \n",
" # Update the title of the figure\n",
" fig.update_layout(title_text='Top 5 countries with highest annual share in global CO₂ Emissions
\\\n",
"Source: Our World in Data')\n",
" \n",
" fig.show()\n",
"print('Successfully defined the compare_annual_emissions function. Run the cell below and click the button.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Layout for widgets\n",
"box_layout = Layout(display='flex', flex_flow='row', align_items='center', width='100%', justify_content = 'center')\n",
"style = {'description_width': 'initial'}\n",
"\n",
"# Create dropdown menu for Year to be used for comparison\n",
"year1_menu = widgets.Dropdown(options = df2['Year'].sort_values().unique(), description ='Year 1: ', style = style, disabled=False)\n",
"year2_menu = widgets.Dropdown(options = df2['Year'].sort_values().unique(), description ='Year 2: ', style = style, disabled=False)\n",
"\n",
"# Create \"Show CO₂ Emissions\" button and define click events\n",
"show_button = widgets.Button(button_style= 'info', description=\"Show CO₂ Emitters\")\n",
"show_button.on_click(compare_annual_emissions)\n",
"\n",
"# Define display order for the buttons and menus\n",
"display(Box(children = [year1_menu, year2_menu], layout = box_layout))\n",
"display(VBox(children = [show_button], layout = Layout(display= 'flex', flex_flow= 'column', align_items= 'center', width='100%', justify_content = 'center')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hover the mouse around to see the comparative annual contribution of the countries towards global CO2 emissions for the selected years. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Questions:\n",
"1. Compare the CO2 emissions of countries after World War I (1919) and II (1945). How have the shares changed for countries on different sides of those conflicts?\n",
"2. Have the top 5 contributors to global CO2 emissions changed in the era of contemporary globalization (from 1945 to 2017)?\n",
"\n",
"### Discussion:\n",
"3. What steps could governments take to reduce their share of CO2 emissions and ensure sustainable globalization?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[](https://github.com/callysto/curriculum-notebooks/blob/master/LICENSE.md)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"nbTranslate": {
"displayLangs": [
"*"
],
"hotkey": "alt-t",
"langInMainMenu": true,
"sourceLang": "en",
"targetLang": "fr",
"useGoogleTranslate": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}