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

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

.. _sphx_glr_gallery_statistics_histogram_bihistogram.py:


===========
Bihistogram
===========

How to plot a bihistogram with Matplotlib.

.. GENERATED FROM PYTHON SOURCE LINES 8-15

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    # Create a random number generator with a fixed seed for reproducibility
    rng = np.random.default_rng(19680801)








.. GENERATED FROM PYTHON SOURCE LINES 16-22

Generate data and plot a bihistogram
------------------------------------

To generate a bihistogram we need two datasets (each being a vector of numbers).
We will plot both histograms using plt.hist() and set the weights of the second
one to be negative. We'll generate data below and plot the bihistogram.

.. GENERATED FROM PYTHON SOURCE LINES 22-47

.. code-block:: Python


    N_points = 10_000

    # Generate two normal distributions
    dataset1 = np.random.normal(0, 1, size=N_points)
    dataset2 = np.random.normal(1, 2, size=N_points)

    # Use a constant bin width to make the two histograms easier to compare visually
    bin_width = 0.25
    bins = np.arange(np.min([dataset1, dataset2]),
                        np.max([dataset1, dataset2]) + bin_width, bin_width)

    fig, ax = plt.subplots()

    # Plot the first histogram
    ax.hist(dataset1, bins=bins, label="Dataset 1")

    # Plot the second histogram
    # (notice the negative weights, which flip the histogram upside down)
    ax.hist(dataset2, weights=-np.ones_like(dataset2), bins=bins, label="Dataset 2")
    ax.axhline(0, color="k")
    ax.legend()

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_bihistogram_001.png
   :alt: histogram bihistogram
   :srcset: /gallery/statistics/images/sphx_glr_histogram_bihistogram_001.png, /gallery/statistics/images/sphx_glr_histogram_bihistogram_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 48-49

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


.. _sphx_glr_download_gallery_statistics_histogram_bihistogram.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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