.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/plot_bar.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_plot_bar.py: ================== Stacked bar charts ================== .. GENERATED FROM PYTHON SOURCE LINES 7-10 .. code-block:: Python # sphinx_gallery_thumbnail_number = 4 .. GENERATED FROM PYTHON SOURCE LINES 11-21 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. .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Python import matplotlib.pyplot as plt import pyam from pyam.plotting import add_net_values_to_bar_plot df = pyam.IamDataFrame("tutorial_data.csv") df .. rst-class:: sphx-glr-script-out .. code-block:: none /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. 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) .. GENERATED FROM PYTHON SOURCE LINES 31-39 Show stacked bar chart by categories ************************************ First, we generate a simple stacked bar chart of all components of primary energy supply for one scenario. Calling :func:`tight_layout() ` ensures that the final plot looks nice and tidy. .. GENERATED FROM PYTHON SOURCE LINES 39-48 .. code-block:: Python args = dict(model="WITCH-GLOBIOM 4.4", scenario="CD-LINKS_NPi2020_1000") data = df.filter(**args, variable="Primary Energy|*", region="World") data.plot.bar(stacked=True, title="Primary energy mix") plt.legend(loc=1) plt.tight_layout() plt.show() .. image-sg:: /gallery/images/sphx_glr_plot_bar_001.png :alt: Primary energy mix :srcset: /gallery/images/sphx_glr_plot_bar_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-53 Flip the direction of a stacked bar chart ***************************************** We can flip that round for a horizontal chart. .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: Python data.plot.bar(stacked=True, orient="h", title="Primary energy mix") plt.legend(loc=1) plt.tight_layout() plt.show() .. image-sg:: /gallery/images/sphx_glr_plot_bar_002.png :alt: Primary energy mix :srcset: /gallery/images/sphx_glr_plot_bar_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-66 Show stacked bar chart by regions ********************************* We don't just have to plot subcategories of variables, any data or meta indicators from the IamDataFrame can be used. Here, we show the contribution by region to total CO2 emissions. .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python data = df.filter(**args, variable="Emissions|CO2").filter(region="World", keep=False) data.plot.bar( bars="region", stacked=True, title="CO2 emissions by region", cmap="tab20" ) plt.legend(loc=1) plt.tight_layout() plt.show() .. image-sg:: /gallery/images/sphx_glr_plot_bar_003.png :alt: CO2 emissions by region :srcset: /gallery/images/sphx_glr_plot_bar_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-82 Add indicators to show net values ********************************* Sometimes, stacked bar charts have negative entries. In that case, it helps to add a line showing the net value. .. GENERATED FROM PYTHON SOURCE LINES 82-91 .. code-block:: Python fig, ax = plt.subplots() data.plot.bar( ax=ax, bars="region", stacked=True, title="CO2 emissions by region", cmap="tab20" ) add_net_values_to_bar_plot(ax) plt.legend(loc=1) plt.tight_layout() plt.show() .. image-sg:: /gallery/images/sphx_glr_plot_bar_004.png :alt: CO2 emissions by region :srcset: /gallery/images/sphx_glr_plot_bar_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.052 seconds) .. _sphx_glr_download_gallery_plot_bar.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bar.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_bar.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_