Add error bars to a Matplotlib bar plot

Python

Matplotlib

Bar

Add error bars

Luc B.

Python

Matplotlib

Bar Plot

Error bars are essential for expressing uncertainty in data so you don't mislead readers.

Code Example

Use the yerr parameter to plt.bar() to add error bars.

import matplotlib.pyplot as plt

labels = ['Amy', 'Rob', 'Dimitri', 'Sarah', 'Brian']
values = [60, 56, 63, 75, 48]

# Add error bars here
plt.bar(labels, values, yerr=[10, 5, 10, 7, 4])

plt.ylabel('Typing Speed (wpm)')
plt.show()

png

If you're using a horizontal bar chart, use the xerr parameter instead.

More Examples

Add Top and Bottom Bars

Sometimes it's nice to have horizontal bars on the top and bottom of your error bars. You can add these with the capsize parameter to plt.bar().

# Add error bars caps here
plt.bar(labels, values, yerr=[10, 5, 10, 7, 4], capsize=5)

plt.ylabel('Typing Speed (wpm)')
plt.show()

png