Add perpendicular caps to error bars in Matplotlib

Python

Matplotlib

Errorbar

Add caps

Luc B.

Python

Matplotlib

Error Bar

Error bars are usually capped with perpendicular lines to emphasize that the error bar represents an interval. By default, Matplotlib excludes these caps, but they are easily added with plt.errorbar().

Code Example

To add caps to your error bars, use the capsize parameter to plt.errorbar(). capsize is a numerical value representing the cap width.

import matplotlib.pyplot as plt

x_values = [20, 16, 12, 19, 18, 22, 14]
y_values = [1, 0.77, 0.67, 0.85, 0.78, 1.05, 0.63]
y_err = [0.3, 0.2, 0.4, 0.5, 0.3, 0.2, 0.3]

# Add error bar caps here
plt.errorbar(x_values, y_values, yerr=y_err, capsize=5, fmt=".")

plt.show()

png