
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/statistics/histogram_multihist.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_statistics_histogram_multihist.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_statistics_histogram_multihist.py:


=====================================================
The histogram (hist) function with multiple data sets
=====================================================

Plot histogram with multiple sample sets and demonstrate:

* Use of legend with multiple sample sets
* Stacked bars
* Step curve with no fill
* Data sets of different sample sizes

Selecting different bin counts and sizes can significantly affect the
shape of a histogram. The Astropy docs have a great section on how to
select these parameters:
http://docs.astropy.org/en/stable/visualization/histogram.html

.. redirect-from:: /gallery/lines_bars_and_markers/filled_step

.. GENERATED FROM PYTHON SOURCE LINES 22-51

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    np.random.seed(19680801)

    n_bins = 10
    x = np.random.randn(1000, 3)

    fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2)

    colors = ['red', 'tan', 'lime']
    ax0.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)
    ax0.legend(prop={'size': 10})
    ax0.set_title('bars with legend')

    ax1.hist(x, n_bins, density=True, histtype='bar', stacked=True)
    ax1.set_title('stacked bar')

    ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)
    ax2.set_title('stack step (unfilled)')

    # Make a multiple-histogram of data-sets with different length.
    x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
    ax3.hist(x_multi, n_bins, histtype='bar')
    ax3.set_title('different sample sizes')

    fig.tight_layout()
    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_001.png
   :alt: bars with legend, stacked bar, stack step (unfilled), different sample sizes
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_001.png, /gallery/statistics/images/sphx_glr_histogram_multihist_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 52-68

-----------------------------------
Setting properties for each dataset
-----------------------------------

You can style the histograms individually by passing a list of values to the
following parameters:

* edgecolor
* facecolor
* hatch
* linewidth
* linestyle


edgecolor
.........

.. GENERATED FROM PYTHON SOURCE LINES 68-80

.. code-block:: Python


    fig, ax = plt.subplots()

    edgecolors = ['green', 'red', 'blue']

    ax.hist(x, n_bins, fill=False, histtype="step", stacked=True,
            edgecolor=edgecolors, label=edgecolors)
    ax.legend()
    ax.set_title('Stacked Steps with Edgecolors')

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_002.png
   :alt: Stacked Steps with Edgecolors
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_002.png, /gallery/statistics/images/sphx_glr_histogram_multihist_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 81-83

facecolor
.........

.. GENERATED FROM PYTHON SOURCE LINES 83-94

.. code-block:: Python


    fig, ax = plt.subplots()

    facecolors = ['green', 'red', 'blue']

    ax.hist(x, n_bins, histtype="barstacked", facecolor=facecolors, label=facecolors)
    ax.legend()
    ax.set_title("Bars with different Facecolors")

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_003.png
   :alt: Bars with different Facecolors
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_003.png, /gallery/statistics/images/sphx_glr_histogram_multihist_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 95-97

hatch
.....

.. GENERATED FROM PYTHON SOURCE LINES 97-108

.. code-block:: Python


    fig, ax = plt.subplots()

    hatches = [".", "o", "x"]

    ax.hist(x, n_bins, histtype="barstacked", hatch=hatches, label=hatches)
    ax.legend()
    ax.set_title("Hatches on Stacked Bars")

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_004.png
   :alt: Hatches on Stacked Bars
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_004.png, /gallery/statistics/images/sphx_glr_histogram_multihist_004_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 109-111

linewidth
.........

.. GENERATED FROM PYTHON SOURCE LINES 111-124

.. code-block:: Python


    fig, ax = plt.subplots()

    linewidths = [1, 2, 3]
    edgecolors = ["green", "red", "blue"]

    ax.hist(x, n_bins, fill=False, histtype="bar", linewidth=linewidths,
            edgecolor=edgecolors, label=linewidths)
    ax.legend()
    ax.set_title("Bars with Linewidths")

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_005.png
   :alt: Bars with Linewidths
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_005.png, /gallery/statistics/images/sphx_glr_histogram_multihist_005_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 125-127

linestyle
.........

.. GENERATED FROM PYTHON SOURCE LINES 127-139

.. code-block:: Python


    fig, ax = plt.subplots()

    linestyles = ['-', ':', '--']

    ax.hist(x, n_bins, fill=False, histtype='bar', linestyle=linestyles,
            edgecolor=edgecolors, label=linestyles)
    ax.legend()
    ax.set_title('Bars with Linestyles')

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_multihist_006.png
   :alt: Bars with Linestyles
   :srcset: /gallery/statistics/images/sphx_glr_histogram_multihist_006.png, /gallery/statistics/images/sphx_glr_histogram_multihist_006_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 140-148

.. tags:: plot-type: histogram, domain: statistics, purpose: reference

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.hist` / `matplotlib.pyplot.hist`


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_gallery_statistics_histogram_multihist.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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