Errorbars in matplotlib

Here’s how to plot x or y errorbars (or both) and how to customize the resulting plot.

Note: As always, the docstrings are wonderful things. After importing, use ?errorbar at the IPython prompt to view the help.

For these plots, I’m assuming the code in the “Setup” section below has been run (to import pylab and to create some fake data).

  • x is the x data
  • y is the y data
  • e, f, g, and h are different errors. Why four? Because not only can you have horizontal and vertical errorbars but you can have asymmetrical error bars as well. It’s possible to plot four different errors for each point in the four directions -x, +x, -y and +y.

Note that the errors are randomly generated, so the plots below won’t exactly like your plots even if you run the same code.

Getting your own error estimates

Here is an excellent and very readable differences between standard error and standard deviation and when to use each. Guaranteed you’ll feel more comfortable about standard error and standard deviation after reading this.

If you need to generate your own error bars, you can use (for example) the NumPy function std() — but be warned: this function divides by N, not N-1 (degrees of freedom) so be careful with small sample sizes.

The setup (will be assumed for all following code)

If you started IPython with the -pylab switch, the plots should show up immediately when you use the plotting commands below.

If you did not start IPython with the -pylab switch, then you will need to type show() to display the plots. You will also have to close all plots before being able to get back to the command line.


from pylab import *

x = arange(0.1, 4, 0.1)
y = exp(-x)
e = 0.1*abs(randn(len(y)))
f = 0.1*abs(randn(len(y)))
g = 2*e
h = 2*f

Vertical symmetric errorbars

(probably the most common use)
This one has red circles as the marker format, “ro“.


figure()
errorbar(x, y, yerr=e, fmt='ro')
Vertical error bars

Vertical error bars

Horizontal symmetric errorbars

This one has a format of blue dots (“b.”) and instead of using “yerr” uses “xerr

figure()
errorbar(x, y, xerr=f, fmt='b.')
Horizontal error bars

Horizontal error bars

Symmetric x and symmetric y

Back to the red circles. This time, both xerr and yerr are used.


figure()
errorbar(x, y, yerr=e, xerr=f, fmt='ro')
Symmetrical error bars

Symmetrical error bars

Asymmetric error bars

The trick for asymmetric error bars is to pass a list-of-lists (or a 2-D array) as xerr and yerr. The item in the list is for negative error, the second one for positive error. So in this case, -y error is e, +y is g, -x is f, and +x is h.

figure()
errorbar(x, y, yerr=[e,g], xerr=[f,h], fmt='ro')
Aysmmetric error bars

Aysmmetric error bars

Customizing

Here’s an example of the kinds of things you can change in a plot. See the help for plot and errorbar for more.

In this case I made the points be dots, made the line color black, gave the errorbars a red color, made the markers blue, gave the series a label, changed the errorbar cap size to zero, and made the line style a solid line.

Then I added 0.5 to the y values for a quick way of getting a second line. I plotted this second line with different formatting commands.

Then I added x and y axis labels, a legend (which used the labels provided in the plotting commands) and finally a title.


errorbar(x, y,
           yerr=e,
           marker='.',
           color='k',
           ecolor='r',
           markerfacecolor='b',
           label="series 1",
           capsize=0,
           linestyle='-')

# Add a second series with different errorbar formatting.
errorbar(x, y+0.5,
           yerr=h,
           marker='o',
           color='k',
           ecolor='k',
           markerfacecolor='g',
           label="series 2",
           capsize=5,
           linestyle='None')

xlabel('x axis label')
ylabel('y axis label')
legend()
title('Customized plot with errorbars')

Yep, it’s ugly . . . please don’t use these formatting choices in real life! My intention here is just to show you how to make tweaks to your plot.

Custom error bars

Custom error bars

2 Responses to “Errorbars in matplotlib”


Leave a Reply