How to Create Error Bar Plot in Matplotlib: Simple Guide
Use the
matplotlib.pyplot.errorbar() function to create error bar plots by passing your data points and their corresponding error values. Specify x, y, and yerr (or xerr) to show vertical or horizontal error bars respectively.Syntax
The basic syntax of errorbar() is:
x: x-coordinates of data points.y: y-coordinates of data points.yerr: vertical error values (can be scalar, array, or list).xerr: horizontal error values (optional).fmt: format string for data points (e.g., 'o' for circles).ecolor: color of error bars.
This function plots data points with error bars showing uncertainty or variability.
python
matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None, capsize=None, capthick=None, elinewidth=None, errorevery=1, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, **kwargs)
Example
This example shows how to plot points with vertical error bars using yerr. It demonstrates data points with symmetric error bars.
python
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 10) y = np.sin(x) error = 0.2 plt.errorbar(x, y, yerr=error, fmt='o', ecolor='red', capsize=5) plt.title('Error Bar Plot Example') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()
Output
A plot window showing 10 data points as circles along a sine curve with red vertical error bars of length 0.2 and caps at the ends.
Common Pitfalls
Common mistakes when creating error bar plots include:
- Not matching the length of
yerrorxerrarrays to the data points length. - Using
fmtas an empty string which hides data points. - Forgetting to set
capsizeto make error bar caps visible. - Passing error values as negative numbers, which is invalid.
Always ensure error arrays are positive and correctly sized.
python
import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y = np.random.rand(5) # Wrong: yerr length mismatch # plt.errorbar(x, y, yerr=[0.1, 0.2], fmt='o') # This will raise an error # Right: plt.errorbar(x, y, yerr=0.1, fmt='o', capsize=3) plt.show()
Output
A plot window showing 5 data points with uniform vertical error bars of length 0.1 and caps.
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| x | X coordinates of data points | [1, 2, 3, 4] |
| y | Y coordinates of data points | [2, 3, 5, 7] |
| yerr | Vertical error bar sizes | 0.2 or [0.1, 0.2, 0.3, 0.4] |
| xerr | Horizontal error bar sizes | 0.1 or [0.05, 0.1, 0.15, 0.2] |
| fmt | Format of data points | 'o' for circles, '-' for line |
| ecolor | Color of error bars | 'red', 'blue' |
| capsize | Length of error bar caps | 3 or 5 |
Key Takeaways
Use matplotlib.pyplot.errorbar() with x, y, and yerr to create error bar plots.
Ensure error arrays match data length and contain positive values.
Set fmt to show data points and capsize to make error bar caps visible.
You can add horizontal error bars using xerr parameter.
Customize error bar color with ecolor for better visualization.