{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Create boxes from error bars using PatchCollection\n\nIn this example, we snazz up a pretty standard error bar plot by adding\na rectangle patch defined by the limits of the bars in both the x- and\ny- directions. To do this, we have to write our own custom function\ncalled ``make_error_boxes``. Close inspection of this function will\nreveal the preferred pattern in writing functions for matplotlib:\n\n1. an `~.axes.Axes` object is passed directly to the function\n2. the function operates on the ``Axes`` methods directly, not through\n the ``pyplot`` interface\n3. plotting keyword arguments that could be abbreviated are spelled out for\n better code readability in the future (for example we use *facecolor*\n instead of *fc*)\n4. the artists returned by the ``Axes`` plotting methods are then\n returned by the function so that, if desired, their styles\n can be modified later outside of the function (they are not\n modified in this example).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.collections import PatchCollection\nfrom matplotlib.patches import Rectangle\n\n# Number of data points\nn = 5\n\n# Dummy data\nnp.random.seed(19680801)\nx = np.arange(0, n, 1)\ny = np.random.rand(n) * 5.\n\n# Dummy errors (above and below)\nxerr = np.random.rand(2, n) + 0.1\nyerr = np.random.rand(2, n) + 0.2\n\n\ndef make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r',\n edgecolor='none', alpha=0.5):\n\n # Loop over data points; create box from errors at each point\n errorboxes = [Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum())\n for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T)]\n\n # Create patch collection with specified colour/alpha\n pc = PatchCollection(errorboxes, facecolor=facecolor, alpha=alpha,\n edgecolor=edgecolor)\n\n # Add collection to Axes\n ax.add_collection(pc)\n\n # Plot errorbars\n artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror,\n fmt='none', ecolor='k')\n\n return artists\n\n\n# Create figure and Axes\nfig, ax = plt.subplots(1)\n\n# Call function to create error boxes\n_ = make_error_boxes(ax, x, y, xerr, yerr)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n plot-type: errorbar\n component: rectangle\n component: patchcollection\n 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.errorbar` / `matplotlib.pyplot.errorbar`\n - `matplotlib.axes.Axes.add_collection`\n - `matplotlib.collections.PatchCollection`\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 }