{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Violin plot basics\n\nViolin plots are similar to histograms and box plots in that they show\nan abstract representation of the probability distribution of the\nsample. Rather than showing counts of data points that fall into bins\nor order statistics, violin plots use kernel density estimation (KDE) to\ncompute an empirical distribution of the sample. That computation\nis controlled by several parameters. This example demonstrates how to\nmodify the number of points at which the KDE is evaluated (``points``)\nand how to modify the bandwidth of the KDE (``bw_method``).\n\nFor more information on violin plots and KDE, the scikit-learn docs\nhave a great section: https://scikit-learn.org/stable/modules/density.html\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n\n# fake data\nfs = 10 # fontsize\npos = [1, 2, 4, 5, 7, 8]\ndata = [np.random.normal(0, std, size=100) for std in pos]\n\nfig, axs = plt.subplots(nrows=2, ncols=6, figsize=(10, 4))\n\naxs[0, 0].violinplot(data, pos, points=20, widths=0.3,\n showmeans=True, showextrema=True, showmedians=True)\naxs[0, 0].set_title('Custom violin 1', fontsize=fs)\n\naxs[0, 1].violinplot(data, pos, points=40, widths=0.5,\n showmeans=True, showextrema=True, showmedians=True,\n bw_method='silverman')\naxs[0, 1].set_title('Custom violin 2', fontsize=fs)\n\naxs[0, 2].violinplot(data, pos, points=60, widths=0.7, showmeans=True,\n showextrema=True, showmedians=True, bw_method=0.5)\naxs[0, 2].set_title('Custom violin 3', fontsize=fs)\n\naxs[0, 3].violinplot(data, pos, points=60, widths=0.7, showmeans=True,\n showextrema=True, showmedians=True, bw_method=0.5,\n quantiles=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]])\naxs[0, 3].set_title('Custom violin 4', fontsize=fs)\n\naxs[0, 4].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,\n showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)\naxs[0, 4].set_title('Custom violin 5', fontsize=fs)\n\naxs[0, 5].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,\n showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='low')\n\naxs[0, 5].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,\n showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')\naxs[0, 5].set_title('Custom violin 6', fontsize=fs)\n\naxs[1, 0].violinplot(data, pos, points=80, orientation='horizontal', widths=0.7,\n showmeans=True, showextrema=True, showmedians=True)\naxs[1, 0].set_title('Custom violin 7', fontsize=fs)\n\naxs[1, 1].violinplot(data, pos, points=100, orientation='horizontal', widths=0.9,\n showmeans=True, showextrema=True, showmedians=True,\n bw_method='silverman')\naxs[1, 1].set_title('Custom violin 8', fontsize=fs)\n\naxs[1, 2].violinplot(data, pos, points=200, orientation='horizontal', widths=1.1,\n showmeans=True, showextrema=True, showmedians=True,\n bw_method=0.5)\naxs[1, 2].set_title('Custom violin 9', fontsize=fs)\n\naxs[1, 3].violinplot(data, pos, points=200, orientation='horizontal', widths=1.1,\n showmeans=True, showextrema=True, showmedians=True,\n quantiles=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]],\n bw_method=0.5)\naxs[1, 3].set_title('Custom violin 10', fontsize=fs)\n\naxs[1, 4].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',\n widths=1.1, showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)\naxs[1, 4].set_title('Custom violin 11', fontsize=fs)\n\naxs[1, 5].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',\n widths=1.1, showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='low')\n\naxs[1, 5].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',\n widths=1.1, showmeans=True, showextrema=True, showmedians=True,\n quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')\naxs[1, 5].set_title('Custom violin 12', fontsize=fs)\n\n\nfor ax in axs.flat:\n ax.set_yticklabels([])\n\nfig.suptitle(\"Violin Plotting Examples\")\nfig.subplots_adjust(hspace=0.4)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: violin, domain: statistics\n\n.. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.violinplot` / `matplotlib.pyplot.violinplot`\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }