anthroposouth/anthroposouth_visuals_version_2.ipynb
2025-10-28 08:02:03 +01:00

396 lines
12 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "c09834e2-1f4d-4b97-b5bb-50f8940bfcab",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import csv\n",
"import matplotlib\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"id": "67f7914b-a7a7-4d96-b92f-9cb6c788f063",
"metadata": {},
"source": [
"# 1 - Oil production\n",
"\n",
"Relative oil production per country between 1900 and 2023"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f8dcf94-577f-4b44-a658-1e9fd07469d4",
"metadata": {},
"outputs": [],
"source": [
"# load data, set index, and workout total production in given unit\n",
"oil = pd.read_csv(\"version_2/input/oilproductionrelative_v2.csv\")\n",
"oil.set_index(\"Year\", inplace=True)\n",
"oil['Total'] = oil.sum(axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19d81163-1d42-407b-bf34-2fe194593285",
"metadata": {},
"outputs": [],
"source": [
"# relative percentage of production into \"country_percent\" columns\n",
"for column in oil:\n",
" oil[column + \"_percent\"] = oil[column] / oil.Total"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49ba427d-7d99-4b11-a121-1bfe1c9aec42",
"metadata": {},
"outputs": [],
"source": [
"# grey palette for black and white printing\n",
"sns.set_palette(\"Greys\")\n",
"\n",
"# decent size and light grey background\n",
"fig, ax = plt.subplots(figsize = (17,8))\n",
"ax.set_facecolor('#fdfdfd')\n",
"\n",
"# actual plot\n",
"ax.stackplot(\n",
" oil.index,\n",
" oil.Argentina_percent,\n",
" oil.Brazil_percent,\n",
" oil.Colombia_percent,\n",
" oil.Ecuador_percent,\n",
" oil.Mexico_percent,\n",
" oil.Venezuela_percent,\n",
" oil.Others_percent\n",
")\n",
"\n",
"# minimalistic x axis with fancy labels\n",
"plt.xticks(np.arange(1900, 2023, 10))\n",
"ax.get_yaxis().set_ticks([])\n",
"ax.tick_params(length=4, color=\"white\")\n",
"ax.tick_params(rotation=35)\n",
"plt.xticks(ha='left')\n",
"\n",
"# no border, clean layout, no non-sense\n",
"ax.spines['top'].set_visible(False)\n",
"ax.spines['right'].set_visible(False)\n",
"ax.spines['bottom'].set_visible(False)\n",
"ax.spines['left'].set_visible(False)\n",
"ax.set_ymargin(0.005)\n",
"ax.set_xmargin(0)\n",
"\n",
"# legend placement is manual (and tricky)\n",
"labels_legend = [\n",
" 'Argentina',\n",
" 'Brazil',\n",
" 'Colombia',\n",
" 'Ecuador',\n",
" 'Mexico',\n",
" 'Venezuela',\n",
" 'Others']\n",
"ax.legend(labels=labels_legend, loc='right', bbox_to_anchor=(0.558, 0.5, 0.545, 0.77), edgecolor=\"white\", reverse=True)\n",
"\n",
"# output to file\n",
"plt.savefig(\"version_2/output/1_oil.png\")"
]
},
{
"cell_type": "markdown",
"id": "6d1ead40-e7ae-4dc3-826e-3c437108080d",
"metadata": {},
"source": [
"# 2 - Selected\n",
"\n",
"Subset of 3 countries with raw amounts\n",
"\n",
"__Important__: match the colors to other graphics"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afdca3f2-4605-4147-a0bb-f7a67af65341",
"metadata": {},
"outputs": [],
"source": [
"# load data, set index\n",
"selected = pd.read_csv(\"final_sources/1-laselected.csv\")\n",
"selected.set_index(\"Year\", inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f051778e-eed5-4288-8027-e62b1c68598d",
"metadata": {},
"outputs": [],
"source": [
"# custom palette to match country colors to first plot\n",
"palette_selected = [\"#d1d1d1\", \"#5c5c5c\", \"#2b2b2b\"]\n",
"sns.set_palette(sns.color_palette(palette_selected))\n",
"\n",
"# all plots with same lightest grey background\n",
"fig, ax = plt.subplots(figsize = (17,8))\n",
"ax.set_facecolor('#fdfdfd')\n",
"\n",
"# actual plot\n",
"ax.stackplot(\n",
" selected.index,\n",
" selected.Brazil,\n",
" selected.Mexico,\n",
" selected.Venezuela\n",
")\n",
"\n",
"# minimalistic x axis with fancy labels\n",
"plt.xticks(np.arange(1900, 2023, 10))\n",
"ax.get_yaxis().set_ticks([])\n",
"ax.tick_params(length=4, color=\"white\")\n",
"ax.tick_params(rotation=35)\n",
"plt.xticks(ha='left')\n",
"\n",
"# no border, clean layout, no non-sense\n",
"ax.spines['top'].set_visible(False)\n",
"ax.spines['right'].set_visible(False)\n",
"ax.spines['bottom'].set_visible(False)\n",
"ax.spines['left'].set_visible(False)\n",
"ax.set_ymargin(0.005)\n",
"ax.set_xmargin(0)\n",
"\n",
"# legend placement is manual (and tricky)\n",
"labels_legend = [\n",
" 'Brazil',\n",
" 'Mexico',\n",
" 'Venezuela',\n",
"]\n",
"ax.legend(labels=labels_legend, loc='right', bbox_to_anchor=(0.558, 0.5, 0.544, 0.91), edgecolor=\"white\", reverse=True)\n",
"\n",
"# output to file\n",
"plt.savefig(\"version_2/output/2_selected.png\")"
]
},
{
"cell_type": "markdown",
"id": "d950448c-503b-4c77-a48a-a4ffcb686d14",
"metadata": {},
"source": [
"# 3 - Raffinage\n",
"Raw data of oil refinment by country of shorter period of time (1940-2023)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "863148f6-5ad4-456a-81ac-ccbbc347a9da",
"metadata": {},
"outputs": [],
"source": [
"# load data, set index\n",
"raffinage = pd.read_csv(\"version_2/input/2-refining.csv\")\n",
"raffinage.set_index(\"Year\", inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44fd6c05-2fd9-4a71-8ff3-28e35eb2a4fb",
"metadata": {},
"outputs": [],
"source": [
"# Same palette as first (oil) plot but Ecuador is now Peru\n",
"sns.set_palette(\"Greys\")\n",
"\n",
"# same size and background color\n",
"fig, ax = plt.subplots(figsize = (17,8))\n",
"ax.set_facecolor('#fdfdfd')\n",
"\n",
"# actual plot\n",
"ax.stackplot(\n",
" raffinage.index,\n",
" raffinage.Argentina,\n",
" raffinage.Brazil,\n",
" raffinage.Colombia,\n",
" raffinage.Peru,\n",
" raffinage.Mexico,\n",
" raffinage.Venezuela\n",
")\n",
"\n",
"# same minimalist look, update timespan\n",
"plt.xticks(np.arange(1940, 2023, 10))\n",
"ax.get_yaxis().set_ticks([])\n",
"ax.tick_params(length=4, color=\"white\")\n",
"ax.tick_params(rotation=35)\n",
"plt.xticks(ha='left')\n",
"\n",
"# same minimalist look\n",
"ax.spines['top'].set_visible(False)\n",
"ax.spines['right'].set_visible(False)\n",
"ax.spines['bottom'].set_visible(False)\n",
"ax.spines['left'].set_visible(False)\n",
"ax.set_ymargin(0.005)\n",
"ax.set_xmargin(0)\n",
"\n",
"# legend\n",
"labels_legend = [\n",
" 'Argentina',\n",
" 'Brazil',\n",
" 'Colombia',\n",
" 'Peru',\n",
" 'Mexico',\n",
" 'Venezuela',\n",
"]\n",
"ax.legend(labels=labels_legend, loc='right', bbox_to_anchor=(0.558, 0.5, 0.545, 0.80), edgecolor=\"white\", reverse=True)\n",
"\n",
"# output to file\n",
"plt.savefig(\"version_2/output/3_raffinage.png\")"
]
},
{
"cell_type": "markdown",
"id": "0d8dd45e-5b6d-4e1d-a283-10e05265b599",
"metadata": {},
"source": [
"# 4 - Energy mix\n",
"General proportion of energy production source from 1800 to 2000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85b04a56-62a4-46db-9b92-499b09177afb",
"metadata": {},
"outputs": [],
"source": [
"# load data, set index, and workout total production in given unit\n",
"mix = pd.read_csv(\"version_2/input/3-energymix.csv\")\n",
"mix.set_index(\"Year\", inplace=True)\n",
"mix['Total'] = mix.sum(axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d05090cd-2c40-4920-88df-97b8d2aec519",
"metadata": {},
"outputs": [],
"source": [
"# workout percentages in their columns\n",
"for column in mix:\n",
" mix[column + \"_percent\"] = mix[column] / mix.Total"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a446974-6469-44ac-b406-88ab0247bbd1",
"metadata": {},
"outputs": [],
"source": [
"# custom palette to prevent dark grey \"biomass\" to empty printer black ink\n",
"paletto = [\n",
" \"#ededed\",\n",
" \"#d1d1d1\",\n",
" \"#adadad\",\n",
" \"#828282\",\n",
" \"#5c5c5c\"\n",
"]\n",
"\n",
"# same settings as before\n",
"fig, ax = plt.subplots(figsize = (17,8))\n",
"ax.set_facecolor('#fdfdfd')\n",
"\n",
"# actual plot\n",
"ax.stackplot(\n",
" mix.index,\n",
" mix.Coal_percent,\n",
" mix.Oil_percent,\n",
" mix[\"Natural gas_percent\"],\n",
" mix.Electricity_percent,\n",
" mix.Biomass_percent,\n",
" colors = paletto[::-1]\n",
")\n",
"\n",
"# changed x axis labels anchor\n",
"plt.xticks(np.arange(1800, 2001, 10))\n",
"ax.get_yaxis().set_ticks([])\n",
"ax.tick_params(length=4, color=\"white\")\n",
"ax.tick_params(rotation=35)\n",
"#plt.xticks(ha='left')\n",
"\n",
"# same clean look\n",
"ax.spines['top'].set_visible(False)\n",
"ax.spines['right'].set_visible(False)\n",
"ax.spines['bottom'].set_visible(False)\n",
"ax.spines['left'].set_visible(False)\n",
"ax.set_ymargin(0.005)\n",
"ax.set_xmargin(0)\n",
"\n",
"# legend\n",
"labels_legend = [\n",
" 'Coal',\n",
" 'Oil',\n",
" 'Natural gas',\n",
" 'Electricity',\n",
" 'Biomass'\n",
"]\n",
"ax.legend(labels=labels_legend, loc='right', bbox_to_anchor=(0.558, 0.5, 0.55, 0.832), edgecolor=\"white\", reverse=True)\n",
"\n",
"# output to file\n",
"plt.savefig(\"version_2/output/4_mix.png\")"
]
},
{
"cell_type": "markdown",
"id": "4cb24807-ee27-4f07-a850-a3d86b8ac20f",
"metadata": {},
"source": [
"__Note regarding x axis labels placement__\n",
"\n",
"In most of the plots (oil, raffinage, selected), labels on the x axis are \"anchored left of their ticks\". It means that they are shifted slightly to the right relative to the actual date. Their respective individual tick, placed on the x axis at the precise date, are aligned with the tip of the first character of the label, not the middle, as is the case with the above plot (mix).\n",
"This was done to prevent the first and last label to be visualy misaligned with the frame of the plot. The first label would be halfway out of the frame, and the last (2020) would be entirely inside the frame, as the source data have yearly datapoints until 2023.\n",
"Label have been shifted to visualy mark this imprecision. Conveying a sense of temporal blur, it underlines the function of these visual representations: these are not precise analytical tools, but allow the reader to perceive historical trends in the data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eac58e10-4305-4175-b01c-3744a319e014",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}