Errorbars
In this article we’ll be talking about errorbars. We use them to add information about possible errors or uncertainties. To create a basic errorbar, we use the errorbar method:
In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
# Let's create some random data.
x = np.linspace(0, 20, 20)
y = np.random.randn(20)
# Let's set the uncertainty dy to 0.6.
dy = .6
# Let's plot the data with the uncertainties. The yerr argument determines
# uncertainty and the fmt arguments formats the lines and points just like
# in the plot method. So here, by setting it to 'o', the points will be
# represented by circles.
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=dy, fmt='o')
Out[1]:
Let’s now have a look at some other arguments we can use to fine-tune the errorbars:
In [2]:
# We use color to set the color of the points and ecolor to set
# the color of the errorbars.
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=dy, fmt='o', color='g', ecolor='r')
Out[2]:
In [3]:
# We use elinewidth to set the width of the errorbars.
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=dy, fmt='o', elinewidth=4)
Out[3]:
In [4]:
# By default the errorbars don't have any caps. This is because their
# capsize value is by default set to 0. But you can set it to
# a different value.
fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=dy, fmt='o', capsize=3)
Out[4]:
You can also specify horizontal errorbars by setting the value of xerr:
In [5]:
# horizontal errorbars
dx = .9
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=dx, fmt='o')
Out[5]:
Or even both:
In [6]:
# horizontal and vertical errorbars
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=dx, yerr=dy, fmt='o')
Out[6]: