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

    Click :ref:`here <sphx_glr_download_gallery_lines_bars_and_markers_stackplot_demo.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_lines_bars_and_markers_stackplot_demo.py:


==============
Stackplot Demo
==============

How to create stackplots with Matplotlib.

Stackplots are generated by plotting different datasets vertically on
top of one another rather than overlapping with one another. Below we
show some examples to accomplish this with Matplotlib.



.. code-block:: python

    import numpy as np
    import matplotlib.pyplot as plt

    x = [1, 2, 3, 4, 5]
    y1 = [1, 1, 2, 3, 5]
    y2 = [0, 4, 2, 6, 8]
    y3 = [1, 3, 5, 7, 9]

    y = np.vstack([y1, y2, y3])

    labels = ["Fibonacci ", "Evens", "Odds"]

    fig, ax = plt.subplots()
    ax.stackplot(x, y1, y2, y3, labels=labels)
    ax.legend(loc=2)
    plt.show()

    fig, ax = plt.subplots()
    ax.stackplot(x, y)
    plt.show()




.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /gallery/lines_bars_and_markers/images/sphx_glr_stackplot_demo_001.png
            :class: sphx-glr-multi-img

    *

      .. image:: /gallery/lines_bars_and_markers/images/sphx_glr_stackplot_demo_002.png
            :class: sphx-glr-multi-img




Here we show an example of making a streamgraph using stackplot



.. code-block:: python



    def layers(n, m):
        """
        Return *n* random Gaussian mixtures, each of length *m*.
        """
        def bump(a):
            x = 1 / (.1 + np.random.random())
            y = 2 * np.random.random() - .5
            z = 10 / (.1 + np.random.random())
            for i in range(m):
                w = (i / m - y) * z
                a[i] += x * np.exp(-w * w)
        a = np.zeros((m, n))
        for i in range(n):
            for j in range(5):
                bump(a[:, i])
        return a


    d = layers(3, 100)

    fig, ax = plt.subplots()
    ax.stackplot(range(100), d.T, baseline='wiggle')
    plt.show()



.. image:: /gallery/lines_bars_and_markers/images/sphx_glr_stackplot_demo_003.png
    :class: sphx-glr-single-img





.. _sphx_glr_download_gallery_lines_bars_and_markers_stackplot_demo.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
