Keeling Curve (Mauna Loa CO2)ΒΆ

Plotting the Keeling Curve with atmospheric CO2 measurements from the Mauna Loa NOAA GML baseline station.

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

url = 'https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_weekly_mlo.csv'
df = pd.read_csv(url, skiprows=47, na_values=-999.99)
df.index = pd.to_datetime(df[['year', 'month', 'day']])

ppm_weekly = df['average']
ppm_daily = ppm_weekly.resample('d').interpolate()

_, _, calc_info = rdtools.degradation_year_on_year(ppm_daily)
yoy_changes = calc_info['YoY_values']

moving_avg = ppm_daily.rolling(365, center=True).mean()
ppm_deseasonalized = ppm_daily - moving_avg
grouper = ppm_deseasonalized.groupby(ppm_deseasonalized.index.dayofyear)
median_seasonality = grouper.median()
upper_seasonality = grouper.quantile(0.95)
lower_seasonality = grouper.quantile(0.05)
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(6, 6))

# plot this first because it has a shorter index
yoy_changes.plot(ax=axes[1])
axes[1].set_xlabel(None)
axes[1].set_ylabel('Year-on-year change [%/yr]')
axes[1].grid()

moving_avg.plot(ax=axes[0])
ppm_daily.plot(ax=axes[0])
axes[0].set_ylabel('Dry CO$_2$ Fraction [ppm]')
axes[0].grid()

fig.tight_layout()
keeling curve
fig, ax = plt.subplots()
ax.fill_between(median_seasonality.index, lower_seasonality, upper_seasonality,
                color='grey', label=r'$\mu\pm2\sigma$')
median_seasonality.plot(ax=ax, c='k', ls='--', label='median')
ax.set_xlabel('Day of year [1-365]')
ax.set_ylabel('Seasonal deviation [ppm]')
ax.legend()
fig.tight_layout()
keeling curve

Total running time of the script: ( 0 minutes 11.146 seconds)

Gallery generated by Sphinx-Gallery