Use error bars in a Matplotlib scatter plot

Python

Matplotlib

Scatter

Use error bars

Luc B.

Python

Matplotlib

Scatter Plot

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

Code Example

Use the plt.errorbar() function to create a scatter plot with error bars in Matplotlib.

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]

# Plot error bars here
plt.errorbar(x_values, y_values, yerr=y_err, fmt=".")

plt.show()

png

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.errorbar().

# Change the cap size here
plt.errorbar(x_values, y_values, yerr=y_err, fmt=".", capsize=5)

plt.xlabel('Number of Dance Moves')
plt.ylabel('Cool Factor')
plt.show()

png