Plot error bars in Python with Matplotlib’s plt.errorbar() function by supplying x and y data plus uncertainty magnitudes through xerr or yerr. The same call supports one uncertainty for every point, per-point symmetric values, and separate lower and upper magnitudes.
The examples below use one dataset throughout, so you can change the error input without changing the chart data.
Plot error bars in Python with a complete runnable plt.errorbar() example
Start with symmetric vertical errors. Each value in yerr specifies the same distance above and below its matching y value.
Runnable example: import matplotlib.pyplot as plt; x = [1, 2, 3, 4]; y = [10, 13, 12, 15]; yerr = [1, 1.5, 0.8, 1.2]; plt.errorbar(x, y, yerr=yerr, fmt=’o-‘, capsize=4, color=’navy’, label=’Observed mean’); plt.xlabel(‘Sample’); plt.ylabel(‘Value’); plt.legend(); plt.show()
Here, x, y, and yerr each contain four values, so every point has one matching error magnitude. The fmt=’o-‘ argument draws circular markers connected by a line. The label appears in the legend when plt.legend() is called.
Choose symmetric and asymmetric values for Python error bars
Error values describe distances, not endpoint coordinates. For N plotted points, Matplotlib accepts scalar, one-dimensional, or two-row asymmetric inputs.
- Scalar: yerr=1 applies an error magnitude of 1 to every point.
- One-dimensional: yerr=[1, 1.5, 0.8, 1.2] gives each point its own symmetric magnitude. The input shape is (N,).
- Two-row asymmetric: yerr=[[1, 2, 1, 2], [2, 1, 2, 1]] uses shape (2, N). The first row contains lower magnitudes, and the second contains upper magnitudes.
For asymmetric Python error bars, the lower and upper values must be nonnegative distances from each data point. With the existing dataset, replace the original yerr assignment with the two-row list, then run the same plt.errorbar() call. This creates different uncertainty ranges above and below each marker.
Add horizontal and combined errors with xerr and yerr
Use xerr for horizontal uncertainty. It follows the same scalar, one-dimensional, and (2, N) rules as yerr. For example, this call adds per-point horizontal errors while retaining vertical errors:
plt.errorbar(x, y, xerr=[0.1, 0.2, 0.15, 0.25], yerr=[1, 1.5, 0.8, 1.2], fmt=’o’, capsize=4, label=’Measured values’)
Both arrays must match the four plotted points. A scalar such as xerr=0.2 applies the same horizontal distance everywhere. To make horizontal errors asymmetric, provide a two-row xerr input, such as [[0.1, 0.2, 0.1, 0.2], [0.2, 0.1, 0.2, 0.1]].
Style a Matplotlib errorbar chart with fmt, caps, markers, and legends
The Matplotlib errorbar function separates data styling from error styling, giving you control over readability:
- fmt: Use ‘o’ for markers without a connecting line, ‘o-‘ for markers and a line, or ‘none’ for errors without a data marker or line.
- capsize: Set the cap length in points, such as capsize=5. Use capthick to adjust cap thickness when needed.
- Colors: Set color=’navy’ for the data series and ecolor=’gray’ for the error bars. Add markersize or elinewidth for further control.
- Labels: Use xlabel() and ylabel() for axes. Pass a distinct label to each errorbar call, then call plt.legend() to identify the series.
For multiple datasets, repeat the call with different colors, markers, and labels. Keep the error magnitudes aligned with each dataset’s x and y values so the caps and markers remain correctly paired.


