.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/statistics/errorbars_and_boxes.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_statistics_errorbars_and_boxes.py: ==================================================== Creating boxes from error bars using PatchCollection ==================================================== In this example, we snazz up a pretty standard error bar plot by adding a rectangle patch defined by the limits of the bars in both the x- and y- directions. To do this, we have to write our own custom function called ``make_error_boxes``. Close inspection of this function will reveal the preferred pattern in writing functions for matplotlib: 1. an `~.axes.Axes` object is passed directly to the function 2. the function operates on the ``Axes`` methods directly, not through the ``pyplot`` interface 3. plotting keyword arguments that could be abbreviated are spelled out for better code readability in the future (for example we use *facecolor* instead of *fc*) 4. the artists returned by the ``Axes`` plotting methods are then returned by the function so that, if desired, their styles can be modified later outside of the function (they are not modified in this example). .. GENERATED FROM PYTHON SOURCE LINES 23-72 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from matplotlib.collections import PatchCollection from matplotlib.patches import Rectangle # Number of data points n = 5 # Dummy data np.random.seed(19680801) x = np.arange(0, n, 1) y = np.random.rand(n) * 5. # Dummy errors (above and below) xerr = np.random.rand(2, n) + 0.1 yerr = np.random.rand(2, n) + 0.2 def make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r', edgecolor='none', alpha=0.5): # Loop over data points; create box from errors at each point errorboxes = [Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum()) for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T)] # Create patch collection with specified colour/alpha pc = PatchCollection(errorboxes, facecolor=facecolor, alpha=alpha, edgecolor=edgecolor) # Add collection to axes ax.add_collection(pc) # Plot errorbars artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, fmt='none', ecolor='k') return artists # Create figure and axes fig, ax = plt.subplots(1) # Call function to create error boxes _ = make_error_boxes(ax, x, y, xerr, yerr) plt.show() .. image-sg:: /gallery/statistics/images/sphx_glr_errorbars_and_boxes_001.png :alt: errorbars and boxes :srcset: /gallery/statistics/images/sphx_glr_errorbars_and_boxes_001.png, /gallery/statistics/images/sphx_glr_errorbars_and_boxes_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-81 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.axes.Axes.errorbar` / `matplotlib.pyplot.errorbar` - `matplotlib.axes.Axes.add_collection` - `matplotlib.collections.PatchCollection` .. _sphx_glr_download_gallery_statistics_errorbars_and_boxes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: errorbars_and_boxes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: errorbars_and_boxes.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_