208 lines
88 KiB
Plaintext
208 lines
88 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "6eef6f4d-dfdf-4e16-bcac-5ba70356672a",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Import libraries"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"id": "adc16d7f-d534-45a6-b507-943069060c31",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import pandas as pd\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "5bfcfe17-df31-4683-b3ab-2b37e957b2df",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# User input"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"id": "4325a3e9-4152-41ff-a301-4399d84f6489",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"year_born = 1987"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "06d5d32f-85c2-4bab-bfcf-4a731d10e1ed",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Get the data"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 27,
|
||
|
"id": "63ef2548-e996-4608-9547-ef512ac16963",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# The code below fetches the data from the \"Our World in Data\" Github page. \n",
|
||
|
"# This is the data behind this graph/table: https://ourworldindata.org/grapher/cumulative-co-emissions\n",
|
||
|
"# If the url does not work you can also use the data provided, by uncommenting the line below.\n",
|
||
|
"df = pd.read_csv('./data/owid-co2-data.csv', header =0)\n",
|
||
|
"# df = pd.read_csv('https://nyc3.digitaloceanspaces.com/owid-public/data/co2/owid-co2-data.csv', header=0)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "16d9f463-a017-4ac1-8f9c-e8c7c333408b",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Process the data"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 28,
|
||
|
"id": "c45399e7-da7d-4c12-a75c-51761a9ac8e9",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Select only data for World\n",
|
||
|
"df = df[df['country'] == 'World']\n",
|
||
|
"\n",
|
||
|
"# Select year and Cumulative CO2\n",
|
||
|
"df = df[['year','cumulative_co2']]\n",
|
||
|
"\n",
|
||
|
"# Turn year to integer\n",
|
||
|
"df['year'] = df['year'].astype(int)\n",
|
||
|
"\n",
|
||
|
"# Subtract from every cumulative value what has been emitted until 1750\n",
|
||
|
"df['cumulative_co2']=df['cumulative_co2']-df['cumulative_co2'].iloc[0]\n",
|
||
|
"\n",
|
||
|
"# For every row:\n",
|
||
|
"# 1. Calculate difference between \"cumulative CO2 in year i\" and \"cumulative CO2 in last year\" \n",
|
||
|
"# 2. Divide this difference with \"cumulative CO2 in last year\" \n",
|
||
|
"\n",
|
||
|
"sums = []\n",
|
||
|
"\n",
|
||
|
"for i in range(len(df)):\n",
|
||
|
" share_gen = (df['cumulative_co2'].iloc[-1]-df['cumulative_co2'].iloc[i])/df['cumulative_co2'].iloc[-1]*100\n",
|
||
|
" sums.append(share_gen)\n",
|
||
|
"\n",
|
||
|
"# Add the new column of sums to the original DataFrame\n",
|
||
|
"df['share_gen'] = sums\n",
|
||
|
"\n",
|
||
|
"# Select every fifth row\n",
|
||
|
"df = df.iloc[::5]\n",
|
||
|
"\n",
|
||
|
"# Select data for people who were born in the last 100 years\n",
|
||
|
"scope = int(100/5)\n",
|
||
|
"df = df.tail(scope).reset_index(drop=True)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c4522817-24cb-4a71-b49f-501061a2e5f4",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Plotting"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 29,
|
||
|
"id": "b690b7ab-388f-4a0f-84cd-bbaceab2f7fb",
|
||
|
"metadata": {
|
||
|
"scrolled": true
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAHeCAYAAAAW80bnAAAAP3RFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMS5wb3N0MSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8kixA/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAD0J0lEQVR4nOzddVhVycPA8e+lGwEJkRRUMLEbxQAVa0VR18Au0AW72xW7W1fstXXXxlastcXARCxswEDyvH8I5+USign6m8/z8OidM2fOzOHcy9xJhSRJEoIgCIIgCMJPTyWnMyAIgiAIgiB8G6JiJwiCIAiC8IsQFTtBEARBEIRfhKjYCYIgCIIg/CJExU4QBEEQBOEXISp2giAIgiAIvwhRsRMEQRAEQfhFiIqdIAiCIAjCL0JU7ARBEARBEH4RomInCIKS3bt34+LigpaWFgqFgqioqK9O087Ojvbt23/2eYcOHUKhULBx48avzkOqUaNGoVAovll6/wtq1KhBjRo1vnm67du3x87O7pun+6P87PkXfk2iYid8N0FBQSgUCvlHS0uLQoUK4efnx5MnT3I6e1/t6tWrjBo1ivDw8JzOyjfz4sULvL290dbWZu7cuaxcuRJdXd2czlaOOnToEE2bNsXCwgINDQ3MzMxo2LAhmzdvzhD3xYsX9O/fn8KFC6OlpYWxsTEeHh5s3749Q9z79+8zevRoypcvj5GREXnz5qVGjRrs27fvRxRL+B+wd+9eOnXqRLFixVBVVc2yEpr6ZSern5CQEDl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Gn69Ok
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Create a horizontal barplot of year against cumulative_co2\n",
|
||
|
"ax = df.plot.barh(x='year', y='share_gen', color = 'silver', width=0.8, linestyle = \"\")\n",
|
||
|
"\n",
|
||
|
"# Add rasters to the plot\n",
|
||
|
"ax.grid(axis='x', linestyle='-', alpha=0.5, color = 'white', zorder=1)\n",
|
||
|
"ax.grid(axis='y', linestyle='-', alpha=0.5, color = 'white', zorder=1)\n",
|
||
|
"\n",
|
||
|
"# Add names to axes\n",
|
||
|
"ax.set_ylabel('year of birth')\n",
|
||
|
"ax.set_xlabel('percentage')\n",
|
||
|
"\n",
|
||
|
"# Add title\n",
|
||
|
"ax.set_title('Percentage of global CO2 emissions since 1750\\n occurring in your lifetime (last updated: %s)' % df['year'].iloc[-1])\n",
|
||
|
"\n",
|
||
|
"# Turn off the legend\n",
|
||
|
"ax.legend().set_visible(False)\n",
|
||
|
"\n",
|
||
|
"# Turn on raster\n",
|
||
|
"ax.set_xticks(range(0, 101, 10));\n",
|
||
|
"\n",
|
||
|
"# Round the year born to intervals of 5\n",
|
||
|
"year_born_round = int(5 * round(float(year_born)/5))\n",
|
||
|
"\n",
|
||
|
"# Find index in df of year born\n",
|
||
|
"year_born_index = df[df['year'] == year_born_round].index.tolist()[0]\n",
|
||
|
"\n",
|
||
|
"# Add explainer to top right\n",
|
||
|
"props = dict(boxstyle='round', facecolor='lightcoral', alpha=0.5)\n",
|
||
|
"ax.text(0.63, 0.975, \"If you were born in %s,\\n%s%% of the total amount \\nof CO2 that has been \\nemitted since 1750, has \\nbeen emitted in your \\nlifetime.\" % \n",
|
||
|
"(int(df['year'].iloc[year_born_index].item()), \n",
|
||
|
" int(df['share_gen'].iloc[year_born_index].item())), \n",
|
||
|
"transform=ax.transAxes, fontsize=10,\n",
|
||
|
"verticalalignment='top', bbox=props);\n",
|
||
|
"\n",
|
||
|
"# Give the right bar a different color\n",
|
||
|
"ax.patches[year_born_index].set_facecolor('lightcoral');\n",
|
||
|
"\n",
|
||
|
"# Draw the vertical line towards the red bar\n",
|
||
|
"plt.vlines(x = df['share_gen'].iloc[year_born_index], ymin = -1, ymax = year_born_index, color='lightcoral');\n",
|
||
|
"\n",
|
||
|
"plt.figtext(0.06, 0.02, 'https://git.pub.solar/misha/share_CO2_generation', fontsize = 7) \n",
|
||
|
"plt.figtext(0.97, 0.02, 'Original from https://x.com/neilrkaye', ha='right', fontsize = 7) # \n",
|
||
|
"\n",
|
||
|
"plt.tight_layout()\n",
|
||
|
"plt.show()\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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.11.2"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|