Skip to content
Home » matplotlib Part 21 – Errorbars

matplotlib Part 21 – Errorbars

Spread the love

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]:
<ErrorbarContainer object of 3 artists>

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]:
<ErrorbarContainer object of 3 artists>
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]:
<ErrorbarContainer object of 3 artists>
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]:
<ErrorbarContainer object of 3 artists>

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]:
<ErrorbarContainer object of 3 artists>

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]:
<ErrorbarContainer object of 3 artists>

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply