HOWTO · Matplotlib
How to Use Error Bar in Python Matplotlib
One can plot data with error bars using the errorbar() function of Matplotlib in Python.
On this page
Use Matplotlib’s errorbar() function to plot measurements with vertical uncertainty using yerr, horizontal uncertainty using xerr, or both. The examples below use fixed values so you can see how each argument changes the plot.
Python Error Bar
An error bar shows the size of an uncertainty around a plotted measurement. For example, a resistor marked with a tolerance has a range of possible values rather than one guaranteed exact value. In a chart, the bar communicates that range around a data point; it does not change the measured value itself.
Run this example:
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4]
y_values = [1, 2, 3, 4]
y_error = [0.9, 0.5, 0.75, 0.25]
plt.errorbar(x_values, y_values, yerr=y_error)
plt.show()
This draws four points with vertical error bars. yerr supplies the vertical error extent for each corresponding y value. The y_error list has one value per point, so the lengths are aligned with the plotted data. Matplotlib also accepts a shared scalar extent or a two-row array of lower and upper extents; each form must match the data’s shape requirements. Use a scalar when every point has the same symmetric uncertainty, a sequence when each point has its own symmetric uncertainty, or separate lower and upper extents when the uncertainty is asymmetric. The plot contains fixed lists, not randomly generated measurements.
To show horizontal uncertainty as well, pass an xerr value. The following example uses the same four fixed points and gives each point a horizontal extent of 0.5. The resulting x-axis bars are horizontal, while yerr continues to draw vertical bars.
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4]
y_values = [1, 2, 3, 4]
y_error = [0.9, 0.5, 0.75, 0.25]
x_error = [0.5, 0.5, 0.5, 0.5]
plt.errorbar(x_values, y_values, yerr=y_error, xerr=x_error, fmt="--ro", ms=10)
plt.show()
The compact format string in fmt="--ro" combines a dashed line (--), red color (r), and circle markers (o). You can use fmt to choose a data-line style, color, and marker, or use separate plotting options when you need more control. The ms option sets marker size. For marker appearance, mec (also markeredgecolor) changes the outline color, mfc (also markerfacecolor) changes the fill color, and mew (also markeredgewidth) changes outline width. These options affect the data markers, not the error bars.
Error-bar styling has its own arguments. ecolor sets the error-bar color; for instance, ecolor="g" makes the bars green. elinewidth controls the bar line width. capsize sets the length of the small end caps, and capthick controls their thickness. The caps mark the ends of an error bar; they are not the data markers. Set these values to suit the scale and visual weight of your chart instead of relying on an assumed default appearance.
This example combines those settings with the same data, line, and marker styling:
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4]
y_values = [1, 2, 3, 4]
y_error = [0.9, 0.5, 0.75, 0.25]
x_error = [0.5, 0.5, 0.5, 0.5]
plt.errorbar(
x_values,
y_values,
yerr=y_error,
xerr=x_error,
fmt="--ro",
ms=10,
ecolor="g",
elinewidth=3,
capsize=10,
capthick=3,
)
plt.show()
When a chart has many points, showing an error bar at every point can make it harder to read. Use errorevery to select which data points receive error bars while keeping the plotted data line and markers. For example, errorevery=2 displays bars at alternating points, beginning with the first point in the series. It changes the placement of error bars, not the underlying data or the line. The argument can be an integer step or a start-and-step pair when you want to offset the selection.
Run this example to show bars at alternating points while retaining the data line and circle markers:
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4]
y_values = [1, 2, 3, 4]
y_error = [0.9, 0.5, 0.75, 0.25]
plt.errorbar(x_values, y_values, yerr=y_error, fmt="-o", errorevery=2)
plt.show()
Choose the options according to the information in your data: use yerr for uncertainty in the vertical measurement, xerr for uncertainty in the horizontal measurement, and both when both quantities are uncertain. Match each error value to the point it describes, use separate lower and upper extents for asymmetric uncertainty, then adjust fmt, marker options, and error-bar styling for readability. If the plot looks cluttered, errorevery can reduce the number of displayed bars without discarding the plotted observations.