
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/lines_bars_and_markers/bar_stacked.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_lines_bars_and_markers_bar_stacked.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_lines_bars_and_markers_bar_stacked.py:


=================
Stacked bar chart
=================

This is an example of creating a stacked bar plot
using `~matplotlib.pyplot.bar`.

.. GENERATED FROM PYTHON SOURCE LINES 9-38

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    # data from https://allisonhorst.github.io/palmerpenguins/

    species = (
        "Adelie\n $\\mu=$3700.66g",
        "Chinstrap\n $\\mu=$3733.09g",
        "Gentoo\n $\\mu=5076.02g$",
    )
    weight_counts = {
        "Below": np.array([70, 31, 58]),
        "Above": np.array([82, 37, 66]),
    }
    width = 0.5

    fig, ax = plt.subplots()
    bottom = np.zeros(3)

    for boolean, weight_count in weight_counts.items():
        p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)
        bottom += weight_count

    ax.set_title("Number of penguins with above average body mass")
    ax.legend(loc="upper right")

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_bar_stacked_001.png
   :alt: Number of penguins with above average body mass
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_bar_stacked_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_bar_stacked_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 39-43

.. tags::

   plot-type: bar
   level: beginner


.. _sphx_glr_download_gallery_lines_bars_and_markers_bar_stacked.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: bar_stacked.ipynb <bar_stacked.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: bar_stacked.py <bar_stacked.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: bar_stacked.zip <bar_stacked.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
