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

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

.. _sphx_glr_gallery_statistics_histogram_cumulative.py:


========================
Cumulative distributions
========================

This example shows how to plot the empirical cumulative distribution function
(ECDF) of a sample. We also show the theoretical CDF.

In engineering, ECDFs are sometimes called "non-exceedance" curves: the y-value
for a given x-value gives probability that an observation from the sample is
below that x-value. For example, the value of 220 on the x-axis corresponds to
about 0.80 on the y-axis, so there is an 80% chance that an observation in the
sample does not exceed 220. Conversely, the empirical *complementary*
cumulative distribution function (the ECCDF, or "exceedance" curve) shows the
probability y that an observation from the sample is above a value x.

A direct method to plot ECDFs is `.Axes.ecdf`.  Passing ``complementary=True``
results in an ECCDF instead.

Alternatively, one can use ``ax.hist(data, density=True, cumulative=True)`` to
first bin the data, as if plotting a histogram, and then compute and plot the
cumulative sums of the frequencies of entries in each bin.  Here, to plot the
ECCDF, pass ``cumulative=-1``.  Note that this approach results in an
approximation of the E(C)CDF, whereas `.Axes.ecdf` is exact.

.. GENERATED FROM PYTHON SOURCE LINES 26-68

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    np.random.seed(19680801)

    mu = 200
    sigma = 25
    n_bins = 25
    data = np.random.normal(mu, sigma, size=100)

    fig = plt.figure(figsize=(9, 4), layout="constrained")
    axs = fig.subplots(1, 2, sharex=True, sharey=True)

    # Cumulative distributions.
    axs[0].ecdf(data, label="CDF")
    n, bins, patches = axs[0].hist(data, n_bins, density=True, histtype="step",
                                   cumulative=True, label="Cumulative histogram")
    x = np.linspace(data.min(), data.max())
    y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
         np.exp(-0.5 * (1 / sigma * (x - mu))**2))
    y = y.cumsum()
    y /= y[-1]
    axs[0].plot(x, y, "k--", linewidth=1.5, label="Theory")

    # Complementary cumulative distributions.
    axs[1].ecdf(data, complementary=True, label="CCDF")
    axs[1].hist(data, bins=bins, density=True, histtype="step", cumulative=-1,
                label="Reversed cumulative histogram")
    axs[1].plot(x, 1 - y, "k--", linewidth=1.5, label="Theory")

    # Label the figure.
    fig.suptitle("Cumulative distributions")
    for ax in axs:
        ax.grid(True)
        ax.legend()
        ax.set_xlabel("Annual rainfall (mm)")
        ax.set_ylabel("Probability of occurrence")
        ax.label_outer()

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_cumulative_001.png
   :alt: Cumulative distributions
   :srcset: /gallery/statistics/images/sphx_glr_histogram_cumulative_001.png, /gallery/statistics/images/sphx_glr_histogram_cumulative_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 69-78

.. tags:: plot-type: ecdf, plot-type: histogram, domain: statistics

.. admonition:: References

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

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


.. _sphx_glr_download_gallery_statistics_histogram_cumulative.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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