#!/usr/bin/env python3
# usage: uv run owid-life-expectancy1.py
# 
# requirements: matplotlib, pandas

import pandas as pd
import matplotlib.pyplot as plt

url = "https://ourworldindata.org/grapher/life-expectancy.csv"
df = pd.read_csv(url, storage_options={"User-Agent": "Mozilla/5.0"})
countries = ["France",  "United States", "Germany", "United Kingdom"]
endYear = df["Year"].max()

print(df[df["Entity"].isin(countries) &
        (df["Year"] >= 1970)] \
    .pivot(index="Year",
           columns="Entity",
           values="Life expectancy"))

ax = df[df["Entity"].isin(countries) &
        (df["Year"] >= 1970)] \
    .pivot(index="Year",
           columns="Entity",
           values="Life expectancy") \
    .plot(marker="o", figsize=(12, 5),
          title=f"Life Expectancy 1970–{endYear}")

plt.ylabel("Years")
plt.tight_layout()
plt.savefig("life-expectancy.png", dpi=150)
plt.show()
