{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Artist customization in box plots\n\nThis example demonstrates how to use the various keyword arguments to fully\ncustomize box plots. The first figure demonstrates how to remove and add\nindividual components (note that the mean is the only value not shown by\ndefault). The second figure demonstrates how the styles of the artists can be\ncustomized. It also demonstrates how to set the limit of the whiskers to\nspecific percentiles (lower right Axes)\n\nA good general reference on boxplots and their history can be found here:\nhttps://vita.had.co.nz/papers/boxplots.pdf\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# fake data\nnp.random.seed(19680801)\ndata = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)\nlabels = list('ABCD')\nfs = 10 # fontsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstrate how to toggle the display of different elements:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)\naxs[0, 0].boxplot(data, tick_labels=labels)\naxs[0, 0].set_title('Default', fontsize=fs)\n\naxs[0, 1].boxplot(data, tick_labels=labels, showmeans=True)\naxs[0, 1].set_title('showmeans=True', fontsize=fs)\n\naxs[0, 2].boxplot(data, tick_labels=labels, showmeans=True, meanline=True)\naxs[0, 2].set_title('showmeans=True,\\nmeanline=True', fontsize=fs)\n\naxs[1, 0].boxplot(data, tick_labels=labels, showbox=False, showcaps=False)\ntufte_title = 'Tufte Style \\n(showbox=False,\\nshowcaps=False)'\naxs[1, 0].set_title(tufte_title, fontsize=fs)\n\naxs[1, 1].boxplot(data, tick_labels=labels, notch=True, bootstrap=10000)\naxs[1, 1].set_title('notch=True,\\nbootstrap=10000', fontsize=fs)\n\naxs[1, 2].boxplot(data, tick_labels=labels, showfliers=False)\naxs[1, 2].set_title('showfliers=False', fontsize=fs)\n\nfor ax in axs.flat:\n ax.set_yscale('log')\n ax.set_yticklabels([])\n\nfig.subplots_adjust(hspace=0.4)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstrate how to customize the display different elements:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')\nflierprops = dict(marker='o', markerfacecolor='green', markersize=12,\n markeredgecolor='none')\nmedianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')\nmeanpointprops = dict(marker='D', markeredgecolor='black',\n markerfacecolor='firebrick')\nmeanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')\n\nfig, axs = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)\naxs[0, 0].boxplot(data, boxprops=boxprops)\naxs[0, 0].set_title('Custom boxprops', fontsize=fs)\n\naxs[0, 1].boxplot(data, flierprops=flierprops, medianprops=medianprops)\naxs[0, 1].set_title('Custom medianprops\\nand flierprops', fontsize=fs)\n\naxs[0, 2].boxplot(data, whis=(0, 100))\naxs[0, 2].set_title('whis=(0, 100)', fontsize=fs)\n\naxs[1, 0].boxplot(data, meanprops=meanpointprops, meanline=False,\n showmeans=True)\naxs[1, 0].set_title('Custom mean\\nas point', fontsize=fs)\n\naxs[1, 1].boxplot(data, meanprops=meanlineprops, meanline=True,\n showmeans=True)\naxs[1, 1].set_title('Custom mean\\nas line', fontsize=fs)\n\naxs[1, 2].boxplot(data, whis=[15, 85])\naxs[1, 2].set_title('whis=[15, 85]\\n#percentiles', fontsize=fs)\n\nfor ax in axs.flat:\n ax.set_yscale('log')\n ax.set_yticklabels([])\n\nfig.suptitle(\"I never said they'd be pretty\")\nfig.subplots_adjust(hspace=0.4)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: boxplot, 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.boxplot` / `matplotlib.pyplot.boxplot`\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 }