{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Separate calculation and plotting of boxplots\n\nDrawing a `~.axes.Axes.boxplot` for a given data set, consists of two main operations,\nthat can also be used separately:\n\n1. Calculating the boxplot statistics: `matplotlib.cbook.boxplot_stats`\n2. Drawing the boxplot: `matplotlib.axes.Axes.bxp`\n\nThus, ``ax.boxplot(data)`` is equivalent to ::\n\n stats = cbook.boxplot_stats(data)\n ax.bxp(stats)\n\nAll styling keyword arguments are identical between `~.axes.Axes.boxplot` and\n`~.axes.Axes.bxp`, and they are passed through from `~.axes.Axes.boxplot` to\n`~.axes.Axes.bxp`. However, the *tick_labels* parameter of `~.axes.Axes.boxplot`\ntranslates to a generic *labels* parameter in `.boxplot_stats`, because the labels are\ndata-related and attached to the returned per-dataset dictionaries.\n\nThe following code demonstrates the equivalence between the two methods.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib import cbook\n\nnp.random.seed(19680801)\ndata = np.random.randn(20, 3)\n\nfig, (ax1, ax2) = plt.subplots(1, 2)\n\n# single boxplot call\nax1.boxplot(data, tick_labels=['A', 'B', 'C'],\n patch_artist=True, boxprops={'facecolor': 'bisque'})\n\n# separate calculation of statistics and plotting\nstats = cbook.boxplot_stats(data, labels=['A', 'B', 'C'])\nax2.bxp(stats, patch_artist=True, boxprops={'facecolor': 'bisque'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the separate functions allows to pre-calculate statistics, in case you need\nthem explicitly for other purposes, or to reuse the statistics for multiple plots.\n\nConversely, you can also use the `~.axes.Axes.bxp` function directly, if you already\nhave the statistical parameters:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\nstats = [\n dict(med=0, q1=-1, q3=1, whislo=-2, whishi=2, fliers=[-4, -3, 3, 4], label='A'),\n dict(med=0, q1=-2, q3=2, whislo=-3, whishi=3, fliers=[], label='B'),\n dict(med=0, q1=-3, q3=3, whislo=-4, whishi=4, fliers=[], label='C'),\n]\n\nax.bxp(stats, patch_artist=True, boxprops={'facecolor': 'bisque'})\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: speciality, 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.bxp`\n - `matplotlib.axes.Axes.boxplot`\n - `matplotlib.cbook.boxplot_stats`\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 }