{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Violin plot customization\n\nThis example demonstrates how to fully customize violin plots. The first plot\nshows the default style by providing only the data. The second plot first\nlimits what Matplotlib draws with additional keyword arguments. Then a\nsimplified representation of a box plot is drawn on top. Lastly, the styles of\nthe artists of the violins are modified.\n\nFor more information on violin plots, the scikit-learn docs have a great\nsection: 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\ndef adjacent_values(vals, q1, q3):\n    upper_adjacent_value = q3 + (q3 - q1) * 1.5\n    upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])\n\n    lower_adjacent_value = q1 - (q3 - q1) * 1.5\n    lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)\n    return lower_adjacent_value, upper_adjacent_value\n\n\ndef set_axis_style(ax, labels):\n    ax.set_xticks(np.arange(1, len(labels) + 1), labels=labels)\n    ax.set_xlim(0.25, len(labels) + 0.75)\n    ax.set_xlabel('Sample name')\n\n\n# create test data\nnp.random.seed(19680801)\ndata = [sorted(np.random.normal(0, std, 100)) for std in range(1, 5)]\n\nfig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(9, 3), sharey=True)\n\nax1.set_title('Default violin plot')\nax1.set_ylabel('Observed values')\nax1.violinplot(data)\n\nax2.set_title('Set colors of violins')\nax2.set_ylabel('Observed values')\nax2.violinplot(\n    data,\n    facecolor=[('yellow', 0.3), ('blue', 0.3), ('red', 0.3), ('green', 0.3)],\n    linecolor='black',\n)\n# Note that when passing a sequence of colors, the method will repeat the sequence if\n# less colors are provided than data distributions.\n\nax3.set_title('Customized violin plot')\nparts = ax3.violinplot(\n        data, showmeans=False, showmedians=False, showextrema=False,\n        facecolor='#D43F3A', linecolor='black')\n\nfor pc in parts['bodies']:\n    pc.set_edgecolor('black')\n    pc.set_linewidth(1)\n    pc.set_alpha(1)\n\nquartile1, medians, quartile3 = np.percentile(data, [25, 50, 75], axis=1)\nwhiskers = np.array([\n    adjacent_values(sorted_array, q1, q3)\n    for sorted_array, q1, q3 in zip(data, quartile1, quartile3)])\nwhiskers_min, whiskers_max = whiskers[:, 0], whiskers[:, 1]\n\ninds = np.arange(1, len(medians) + 1)\nax3.scatter(inds, medians, marker='o', color='white', s=30, zorder=3)\nax3.vlines(inds, quartile1, quartile3, color='k', linestyle='-', lw=5)\nax3.vlines(inds, whiskers_min, whiskers_max, color='k', linestyle='-', lw=1)\n\n# set style for the axes\nlabels = ['A', 'B', 'C', 'D']\nfor ax in [ax1, ax2, ax3]:\n    set_axis_style(ax, labels)\n\nplt.subplots_adjust(bottom=0.15, wspace=0.05)\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   - `matplotlib.axes.Axes.vlines` / `matplotlib.pyplot.vlines`\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}