Stacked line charts

# sphinx_gallery_thumbnail_number = 2

Read in tutorial data and show a summary

This gallery uses the scenario data from the first-steps tutorial.

If you haven’t cloned the pyam GitHub repository to your machine, you can download the file from https://github.com/IAMconsortium/pyam/tree/main/docs/tutorials.

Make sure to place the data file in the same folder as this script/notebook.

import matplotlib.pyplot as plt

import pyam

df = pyam.IamDataFrame("tutorial_data.csv")
df
/home/docs/checkouts/readthedocs.org/user_builds/pyam-iamc/checkouts/latest/pyam/utils.py:318: FutureWarning:

The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.


<class 'pyam.core.IamDataFrame'>
Index:
 * model    : AIM/CGE 2.1, GENeSYS-MOD 1.0, ... WITCH-GLOBIOM 4.4 (8)
 * scenario : 1.0, CD-LINKS_INDCi, CD-LINKS_NPi, ... Faster Transition Scenario (8)
Timeseries data coordinates:
   region   : R5ASIA, R5LAM, R5MAF, R5OECD90+EU, R5REF, R5ROWO, World (7)
   variable : ... (6)
   unit     : EJ/yr, Mt CO2/yr, °C (3)
   year     : 2010, 2020, 2030, 2040, 2050, 2060, 2070, 2080, ... 2100 (10)

First, we generate a simple stacked line chart of all components of primary energy supply for one scenario.

model, scenario = "IMAGE 3.0.1", "CD-LINKS_NPi2020_400"

data = df.filter(
    model=model, scenario=scenario, variable="Primary Energy|*", region="World"
)

data.plot.stack(title=scenario)
plt.legend(loc=1)
plt.tight_layout()
plt.show()
CD-LINKS_NPi2020_400
/home/docs/checkouts/readthedocs.org/user_builds/pyam-iamc/checkouts/latest/pyam/plotting.py:466: FutureWarning:

The behavior of array concatenation with empty entries is deprecated. In a future version, this will no longer exclude empty items when determining the result dtype. To retain the old behavior, exclude the empty entries before the concat operation.

We don’t just have to plot subcategories of variables, any data dimension or meta indicators from the IamDataFrame can be used. Here, we show the contribution by region to total CO2 emissions.

data = df.filter(model=model, scenario=scenario, variable="Emissions|CO2").filter(
    region="World", keep=False
)

data.plot.stack(stack="region", cmap="tab20", title=scenario, total=True)
plt.legend(loc=1)
plt.tight_layout()
plt.show()
CD-LINKS_NPi2020_400

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

Gallery generated by Sphinx-Gallery