#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt plt.style.use("ggplot") # In[2]: from pandas_datapackage_reader import read_datapackage from shortcountrynames import to_name # In[3]: df = read_datapackage("https://github.com/openclimatedata/edgar-co2-emissions") # In[4]: unit = "kt" # In[5]: df.head() # In[6]: df = df.reset_index().drop("Name", axis=1).set_index(["Code", "Sector", "Year"]).sort_index() df.head() # In[7]: for code in ["WORLD", "USA", "CHN", "EU28"]: grouped = df.loc[code].reset_index().set_index("Year").groupby("Sector")["Emissions"] fig, axes = plt.subplots(nrows=1, ncols=5, figsize=(12,4), sharey=True) try: name = to_name(code) except KeyError: name = code fig.suptitle(name) sectors = ['Power Industry', 'Transport', 'Buildings', 'Other industrial combustion', 'Non-combustion'] for (key, ax) in zip(sectors, axes): ax.set_title(key, fontsize=10) grouped.get_group(key).plot(ax=ax, legend=False) ax.set_ylabel(unit)