.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/statistics/violinplot.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_statistics_violinplot.py: ================== Violin plot basics ================== Violin plots are similar to histograms and box plots in that they show an abstract representation of the probability distribution of the sample. Rather than showing counts of data points that fall into bins or order statistics, violin plots use kernel density estimation (KDE) to compute an empirical distribution of the sample. That computation is controlled by several parameters. This example demonstrates how to modify the number of points at which the KDE is evaluated (``points``) and how to modify the bandwidth of the KDE (``bw_method``). For more information on violin plots and KDE, the scikit-learn docs have a great section: https://scikit-learn.org/stable/modules/density.html .. GENERATED FROM PYTHON SOURCE LINES 18-89 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) # fake data fs = 10 # fontsize pos = [1, 2, 4, 5, 7, 8] data = [np.random.normal(0, std, size=100) for std in pos] fig, axs = plt.subplots(nrows=2, ncols=5, figsize=(10, 6)) axs[0, 0].violinplot(data, pos, points=20, widths=0.3, showmeans=True, showextrema=True, showmedians=True) axs[0, 0].set_title('Custom violinplot 1', fontsize=fs) axs[0, 1].violinplot(data, pos, points=40, widths=0.5, showmeans=True, showextrema=True, showmedians=True, bw_method='silverman') axs[0, 1].set_title('Custom violinplot 2', fontsize=fs) axs[0, 2].violinplot(data, pos, points=60, widths=0.7, showmeans=True, showextrema=True, showmedians=True, bw_method=0.5) axs[0, 2].set_title('Custom violinplot 3', fontsize=fs) axs[0, 3].violinplot(data, pos, points=60, widths=0.7, showmeans=True, showextrema=True, showmedians=True, bw_method=0.5, quantiles=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]]) axs[0, 3].set_title('Custom violinplot 4', fontsize=fs) axs[0, 4].violinplot(data[-1:], pos[-1:], points=60, widths=0.7, showmeans=True, showextrema=True, showmedians=True, quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5) axs[0, 4].set_title('Custom violinplot 5', fontsize=fs) axs[1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7, showmeans=True, showextrema=True, showmedians=True) axs[1, 0].set_title('Custom violinplot 6', fontsize=fs) axs[1, 1].violinplot(data, pos, points=100, vert=False, widths=0.9, showmeans=True, showextrema=True, showmedians=True, bw_method='silverman') axs[1, 1].set_title('Custom violinplot 7', fontsize=fs) axs[1, 2].violinplot(data, pos, points=200, vert=False, widths=1.1, showmeans=True, showextrema=True, showmedians=True, bw_method=0.5) axs[1, 2].set_title('Custom violinplot 8', fontsize=fs) axs[1, 3].violinplot(data, pos, points=200, vert=False, widths=1.1, showmeans=True, showextrema=True, showmedians=True, quantiles=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]], bw_method=0.5) axs[1, 3].set_title('Custom violinplot 9', fontsize=fs) axs[1, 4].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1, showmeans=True, showextrema=True, showmedians=True, quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5) axs[1, 4].set_title('Custom violinplot 10', fontsize=fs) for ax in axs.flat: ax.set_yticklabels([]) fig.suptitle("Violin Plotting Examples") fig.subplots_adjust(hspace=0.4) plt.show() .. image-sg:: /gallery/statistics/images/sphx_glr_violinplot_001.png :alt: Violin Plotting Examples, Custom violinplot 1, Custom violinplot 2, Custom violinplot 3, Custom violinplot 4, Custom violinplot 5, Custom violinplot 6, Custom violinplot 7, Custom violinplot 8, Custom violinplot 9, Custom violinplot 10 :srcset: /gallery/statistics/images/sphx_glr_violinplot_001.png, /gallery/statistics/images/sphx_glr_violinplot_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-96 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.axes.Axes.violinplot` / `matplotlib.pyplot.violinplot` .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.085 seconds) .. _sphx_glr_download_gallery_statistics_violinplot.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: violinplot.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: violinplot.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_