When creating charts, it’s important to convey not only the central tendency of the data but also its variability. One way to do this is by adding error bars to charts. In this article, we’ll walk you through the process how to  add error bars to chart using Python Matplotlib library. 

We’ll cover everything from the basics to more advanced techniques for customizing your charts.

Understanding Error Bars

Before we get started, let’s make sure we’re all on the same page about what error bars are and why they’re useful. Error bars are a way to visualize the variability in your data. They indicate the range of values that your data could reasonably take, given the amount of variability in your sample.

For example, if you’re measuring the height of a group of people, you might find that the average height is 5’8″. However, not everyone in the group will be exactly 5’8″ tall. Some people will be taller and some will be shorter and this allow you to represent this variability in your data.

Adding Error Bars to a Simple Bar Chart

To add error bars to a bar chart, you need to determine the magnitude of the error for each data point and then display it as a line or rectangle extending above and/or below the bar. One way to do this is to use the errorbar() function in the Matplotlib library.

Here’s an example:

				
					import matplotlib.pyplot as plt

# Example data
x = ['A', 'B', 'C', 'D']
y = [5, 7, 6, 8]
error = [0.5, 0.8, 0.6, 0.7]

# Create a bar chart
plt.bar(x, y)

# Add bars
plt.errorbar(x, y, yerr=error, fmt='o', capsize=5, ecolor='black')

# Add labels and title
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar Chart with Error Bars')

plt.show()

				
			

Output:

Error Bars to Charts in Python

In this code, we first create a basic bar chart using the bar() function. Then, we add error bars using the errorbar() function. We pass in the x, y, and error data, along with some formatting options such as the marker style (fmt), cap size (capsize), and error bar color (ecolor).

Finally, we add labels and a title to the chart using the xlabel(), ylabel(), and title() functions, and then display the chart using the show() function.

Adding Error Bars to a Simple Line Chart

For the same dataset, a line chart with error bars can be made using the code below.

Here’s an example:

				
					import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
error = [0.2, 0.5, 0.3, 0.7, 0.4]

# Create a line chart
plt.plot(x, y)

# Add bars
plt.errorbar(x, y, yerr=error, fmt='-o', capsize=4)

# Add labels and title
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Line Chart with Error Bars')

plt.show()

				
			

Output:

Error Bars to Charts in Python

In this example, we first create a line chart using the plot function. Then, we add error bars using the errorbar function. We pass in the x, y, and error data, along with some formatting options such as the marker style (fmt), cap size (capsize), and error bar color (ecolor).

Finally, we add labels and a title to the chart using the xlabel, ylabel, and title functions, and then display the chart using the show function.

Moreover,  in Matplotlib, you can create both vertical and horizontal error bars using the yerr and xerr arguments, respectively.

Here’s an example of how to create a line chart with horizontal error bars:

				
					import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
error = [0.2, 0.5, 0.3, 0.7, 0.4]

# Create a line chart
plt.plot(x, y)

# Add horizontal 
plt.errorbar(x, y, xerr=error, fmt='-o', capsize=4)

# Add labels and title
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Line Chart with Horizontal Error Bars')

plt.show()

				
			

Output:

Error Bars to Charts in Python

Wrap up

To learn more about Matplotlib library, check out the:
https://matplotlib.org/


Thanks for reading. Happy coding!