0
0
Matplotlibdata~5 mins

Error bar plots in Matplotlib

Choose your learning style9 modes available
Introduction

Error bar plots help us see how much uncertainty or variation there is in data points. They show the main value and how much it might change.

When you want to show the average height of plants and how much heights vary.
When comparing test scores of students with possible score ranges.
When showing daily temperatures with possible measurement errors.
When presenting survey results with confidence intervals.
When visualizing scientific measurements with known errors.
Syntax
Matplotlib
matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None, capsize=None, label=None)

x and y are the data points to plot.

yerr and xerr set the size of error bars vertically and horizontally.

Examples
Basic vertical error bars showing uncertainty in y values.
Matplotlib
plt.errorbar(x, y, yerr=error)
Plot points as circles with vertical error bars.
Matplotlib
plt.errorbar(x, y, yerr=error, fmt='o')
Show both vertical and horizontal error bars with square markers, red error bars, and caps on error lines.
Matplotlib
plt.errorbar(x, y, yerr=error, xerr=error_x, fmt='s', ecolor='red', capsize=5)
Sample Program

This code plots points with vertical error bars showing uncertainty. Blue error bars have caps for clarity. The plot has labels and a grid for easy reading.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2.0, 3.5, 2.8, 4.2, 5.0])
y_error = np.array([0.2, 0.3, 0.15, 0.4, 0.25])

plt.errorbar(x, y, yerr=y_error, fmt='o', ecolor='blue', capsize=4, label='Data with error')
plt.title('Error Bar Plot Example')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Use capsize to add small horizontal lines at the ends of error bars for better visibility.

Set fmt to control marker style; empty string '' means no markers.

Error bars can be symmetric (single number) or asymmetric (tuple or array with two values).

Summary

Error bar plots show data points with uncertainty ranges.

Use yerr and xerr to add vertical and horizontal error bars.

Customize markers, colors, and caps to make plots clear and informative.