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

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

.. _sphx_glr_gallery_statistics_histogram_features.py:


==============================================
Some features of the histogram (hist) function
==============================================

In addition to the basic histogram, this demo shows a few optional features:

* Setting the number of data bins.
* The *density* parameter, which normalizes bin heights so that the integral of
  the histogram is 1. The resulting histogram is an approximation of the
  probability density function.

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.

.. _section: http://docs.astropy.org/en/stable/visualization/histogram.html

.. GENERATED FROM PYTHON SOURCE LINES 19-50

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    rng = np.random.default_rng(19680801)

    # example data
    mu = 106  # mean of distribution
    sigma = 17  # standard deviation of distribution
    x = rng.normal(loc=mu, scale=sigma, size=420)

    num_bins = 42

    fig, ax = plt.subplots()

    # the histogram of the data
    n, bins, patches = ax.hist(x, num_bins, density=True)

    # add a 'best fit' line
    y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
         np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
    ax.plot(bins, y, '--')
    ax.set_xlabel('Value')
    ax.set_ylabel('Probability density')
    ax.set_title('Histogram of normal distribution sample: '
                 fr'$\mu={mu:.0f}$, $\sigma={sigma:.0f}$')

    # Tweak spacing to prevent clipping of ylabel
    fig.tight_layout()
    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_histogram_features_001.png
   :alt: Histogram of normal distribution sample: $\mu=106$, $\sigma=17$
   :srcset: /gallery/statistics/images/sphx_glr_histogram_features_001.png, /gallery/statistics/images/sphx_glr_histogram_features_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 51-60

.. 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.set_title`
   - `matplotlib.axes.Axes.set_xlabel`
   - `matplotlib.axes.Axes.set_ylabel`


.. _sphx_glr_download_gallery_statistics_histogram_features.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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