Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add error bars in matplotlib

Understanding Error Bars

When you're getting started with data visualization, one of the key aspects you'll want to represent is the uncertainty or variability in your data. Error bars are graphical representations of the variability of data and are used on graphs to indicate the error or uncertainty in a reported measurement. They give a general idea of how precise a measurement is, or conversely, how far from the reported value the true (error-free) value might be.

Imagine you're measuring the height of a plant over several days. Each day, you might get a slightly different measurement for various reasons: maybe your ruler slips a bit, or the plant is leaning slightly. Error bars on a graph of your measurements would show these slight variations, giving a visual sense of how 'spread out' your measurements are.

Adding Basic Error Bars

In Python, the matplotlib library is a popular tool for creating graphs, charts, and visualizations. Let's start by adding simple error bars to a line chart. First, you'll need to import matplotlib.pyplot which is a module in Matplotlib that provides functions to add plot elements like text, lines, and error bars.

import matplotlib.pyplot as plt

Now, let's create some data to plot:

# Sample data
x = [1, 2, 3, 4]
y = [2, 3, 5, 7]
yerr = [0.1, 0.2, 0.3, 0.4]  # Error values for each data point

Here, x and y are lists representing the x and y coordinates of our data points, and yerr is a list of the error values for each y-coordinate. To add error bars to the plot, we'll use the errorbar function:

plt.errorbar(x, y, yerr=yerr, fmt='o', color='black', ecolor='lightgray', elinewidth=3, capsize=0)
plt.show()

In the above code, fmt='o' sets the format of the data points to circles, color='black' sets the color of the data points, ecolor='lightgray' sets the color of the error bars, elinewidth=3 sets the line width of the error bars, and capsize=0 removes the caps on the ends of the error bars.

Customizing Error Bars

You can make your error bars more informative by customizing them further. For example, you can add horizontal error bars if your x-values also have uncertainty, or make the error bars different for each point if the uncertainties are not uniform.

Here's how to add horizontal error bars:

xerr = [0.2, 0.15, 0.3, 0.1]  # Error values for each x-coordinate

plt.errorbar(x, y, xerr=xerr, yerr=yerr, fmt='o', color='black', ecolor='red', elinewidth=3, capsize=5)
plt.show()

Notice that we've added xerr for the x-errors and changed the ecolor to red. The capsize parameter has been set to 5 to add caps to the ends of the error bars, making them look like little T-bars that clearly mark the range of the error.

Error Bars with Subplots

Sometimes you may want to show multiple plots with error bars together. This is where subplots come in handy. Subplots are a way to arrange multiple plots in a grid within a single figure.

Here's an example of creating two subplots:

# Create a figure and a 2x1 grid of subplots
fig, (ax1, ax2) = plt.subplots(2, 1)

# First subplot
ax1.errorbar(x, y, yerr=yerr, fmt='o', color='black', ecolor='lightgray', elinewidth=3, capsize=0)
ax1.set_title('Vertical Error Bars')

# Second subplot
ax2.errorbar(x, y, xerr=xerr, yerr=yerr, fmt='o', color='black', ecolor='red', elinewidth=3, capsize=5)
ax2.set_title('Both Vertical and Horizontal Error Bars')

# Show the figure with subplots
plt.show()

Each subplot is addressed by a different axis object (ax1 and ax2), and you can customize them just like you would with a single plot.

Advanced Error Bars

As you become more comfortable with error bars, you might encounter situations where the error varies in a more complex way. For instance, you might have asymmetric error bars, where the error above the data point is different from the error below it.

Here's how you can do that:

yerr_asymmetric = [[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5]]  # Lower and upper bounds of the y-error

plt.errorbar(x, y, yerr=yerr_asymmetric, fmt='o', color='black', ecolor='blue', elinewidth=3, capsize=5)
plt.show()

In yerr_asymmetric, the first list represents the lower bounds of the error, and the second list represents the upper bounds.

Conclusion

Error bars are a crucial component of data visualization, as they provide a straightforward representation of the variability or uncertainty in your data. In this post, we've explored how to add and customize error bars using Matplotlib in Python. From basic vertical error bars to more complex asymmetric ones, you now have the tools to accurately represent the precision of your measurements in your graphs.

Remember, visualizing data is like telling a story – you want to present the information in a way that is both truthful and easily understood. Error bars help you to tell a more complete story by acknowledging the natural variability in your data. As you continue your journey in programming and data visualization, keep exploring and experimenting with different ways to represent data. With practice, you'll be able to craft clear and informative visual narratives that can inform, persuade, and enlighten your audience.