Skip to content

How to plot error bars from measured standard errors in Matplotlib (with Code)

The Short Answer (TL;DR)

Pass the standard error for each plotted mean to the yerr argument of ax.errorbar. Label the bars as standard errors so readers can distinguish them from standard deviations or confidence intervals.

import matplotlib.pyplot as plt
means = [3.0, 5.0, 4.0]; standard_errors = [0.2, 0.4, 0.3]
plt.errorbar(["A", "B", "C"], means, yerr=standard_errors, fmt="o", capsize=4)

Detailed Implementation

This example uses synthetic data and does not need a network connection. Install the recorded package versions in a separate Python environment before running it.

python -m pip install numpy==2.5.4 pandas==3.0.6 matplotlib==3.11.2 seaborn==0.13.2
import numpy as np
import matplotlib.pyplot as plt

# Synthetic group means and standard errors, supplied in matching order.
x = np.arange(3)
means = np.array([3.0, 5.0, 4.0])
standard_errors = np.array([0.2, 0.4, 0.3])
fig, ax = plt.subplots(figsize=(5.4, 3.5), layout="constrained")
bars = ax.errorbar(x, means, yerr=standard_errors, fmt="o",
                  capsize=5, color="#176b87", ecolor="#176b87")
ax.set_xticks(x, ["A", "B", "C"])
ax.set(xlabel="Group", ylabel="Mean response", title="Group means ± 1 standard error")
fig.savefig("standard-error-bars.png", dpi=160)
plt.show()

The chart shows three means with vertical error bars. Group A is centered at 3.0 and its error bar extends from 2.8 to 3.2.

What does yerr represent?

yerr receives nonnegative error magnitudes, not the absolute upper and lower endpoints. A one-dimensional array gives symmetric bars around each point. For asymmetric errors, use an array with shape (2, N): the first row contains lower magnitudes and the second contains upper magnitudes.

How do standard errors differ from standard deviations?

A standard deviation describes spread among observations. A standard error describes estimated sampling variation of an estimator such as a mean. The same numerical plotting function can display either, so name the quantity in the caption and axes.

Are these bars a 95 percent confidence interval?

The example plots plus and minus one standard error. A confidence interval needs a justified critical multiplier and assumptions about sampling and the estimator. For a small independent normal sample with estimated variance, a mean interval uses a Student t critical value and the appropriate degrees of freedom. Overlapping standard-error bars are not a general significance test.

Mathematical Context

For an independent sample of size n, the usual estimated standard error of the mean is s/√n, where s is the sample standard deviation using n−1 degrees of freedom. Symmetric one-standard-error bars have endpoints mean − SE and mean + SE. In group A, those endpoints are 2.8 and 3.2. Dependence or clustered sampling can require a different standard-error estimator.

\[ \mathrm{SE}(\bar{x})=\frac{s}{\sqrt{n}} \]

Reproducibility

Executed with Python 3.12.14; numpy 2.5.4, pandas 3.0.6, matplotlib 3.11.2, seaborn 0.13.2. Automated checks confirm the stated output properties.

Primary documentation

Related guides