Exercise: Creating a subplot of timeseries (solution)

Exercise: Creating a subplot of timeseries (solution)#

%config InlineBackend.figure_format = 'retina'

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

file_child_mortality = "data/child-mortality.csv"
file_life_expectancy = "data/life-expectancy.csv"
file_poverty = "data/poverty.csv"
file_education = "data/education.csv"
file_literacy = "data/literacy.csv"
file_women_in_office = "data/women-in-office.csv"

# Load the data
child_mortality = pd.read_csv(file_child_mortality)
life_expectancy = pd.read_csv(file_life_expectancy)
poverty = pd.read_csv(file_poverty)
education = pd.read_csv(file_education)
literacy = pd.read_csv(file_literacy)
women_in_office = pd.read_csv(file_women_in_office)

titles = [
    "Children Surviving to Age 5 [%]",
    "Life Expectancy [Years]",
    "People living on more than ($5 a day) [%]",
    "Literacy [%]",
    "People with Some Education [%]",
    "National Legislatures with >10% Women [%]",
]

datasets = [
    child_mortality,
    life_expectancy,
    poverty,
    literacy,
    education,
    women_in_office,
]
fig, axs = plt.subplots(3, 2, figsize=(10, 8))

axs[0, 0].plot(
    child_mortality["Year"], child_mortality["Share surviving first five years"]
)
axs[1, 0].plot(life_expectancy["Year"], life_expectancy["Life Expectancy"])
axs[2, 0].plot(poverty["Year"], poverty["More than $5 a day"])
axs[0, 1].plot(literacy["Year"], literacy["Share of population aged 15+ literate"])
axs[1, 1].plot(
    education["Year"],
    education["Share of population with at least some basic education"],
)
axs[2, 1].plot(women_in_office["Year"], women_in_office["More than 10%"])

# Common formatting changes
for ax, title in zip(axs.T.flatten(), titles):
    ax.set_title(title)
    ax.set_xlim([1820, 2020])
    ax.set_ylim([0, 100])
    ax.set_xticks(np.arange(1820, 2025, 20))

fig.tight_layout()
../../../_images/73a07f5ed58f64a3b2d73365a1abfe970bc54ba81a880d9b821207e62627a802.png