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

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

.. _sphx_glr_gallery_statistics_boxplot_vs_violin.py:


===================================
Box plot vs. violin plot comparison
===================================

Note that although violin plots are closely related to Tukey's (1977)
box plots, they add useful information such as the distribution of the
sample data (density trace).

By default, box plots show data points outside 1.5 * the inter-quartile
range as outliers above or below the whiskers whereas violin plots show
the whole range of the data.

A good general reference on boxplots and their history can be found
here: http://vita.had.co.nz/papers/boxplots.pdf

Violin plots require matplotlib >= 1.4.

For more information on violin plots, the scikit-learn docs have a great
section: https://scikit-learn.org/stable/modules/density.html

.. GENERATED FROM PYTHON SOURCE LINES 22-55

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    # generate some random test data
    all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]

    # plot violin plot
    axs[0].violinplot(all_data,
                      showmeans=False,
                      showmedians=True)
    axs[0].set_title('Violin plot')

    # plot box plot
    axs[1].boxplot(all_data)
    axs[1].set_title('Box plot')

    # adding horizontal grid lines
    for ax in axs:
        ax.yaxis.grid(True)
        ax.set_xticks([y + 1 for y in range(len(all_data))],
                      labels=['x1', 'x2', 'x3', 'x4'])
        ax.set_xlabel('Four separate samples')
        ax.set_ylabel('Observed values')

    plt.show()




.. image-sg:: /gallery/statistics/images/sphx_glr_boxplot_vs_violin_001.png
   :alt: Violin plot, Box plot
   :srcset: /gallery/statistics/images/sphx_glr_boxplot_vs_violin_001.png, /gallery/statistics/images/sphx_glr_boxplot_vs_violin_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 56-65

.. tags:: plot-type: violin, plot-type: boxplot, domain: statistics

.. admonition:: References

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

   - `matplotlib.axes.Axes.boxplot` / `matplotlib.pyplot.boxplot`
   - `matplotlib.axes.Axes.violinplot` / `matplotlib.pyplot.violinplot`


.. _sphx_glr_download_gallery_statistics_boxplot_vs_violin.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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