Create a plot with error bars in Matplotlib





Luc B.



Error Bar

To create accurate visualizations that don't mislead readers, data analysts should always express the uncertainty in their results.

One common technique for this is using error bars. Error bars are data point markers that span a horizontal or vertical window representing the uncertainty of that data point. Matplotlib has an API dedicated to producing error bar plots, plt.errorbar().

Code Example

To create an error bar plot, use plt.errorbar() just as you'd use plt.plot() with the addition of x and/or y error arrays passed as the xerr and yerr parameters.

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]
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=err, fmt=".")


More Examples

Create a Plot with Horizontal Error Bars

As mentioned above, use the xerr parameter to create a plot with horizontal error bars. Note that xerr and yerr can be used simultaneously to create a cross shaped marker that signifies uncertainty along both axes.

# Plot horizontal error bars here
plt.errorbar(x_values, y_values, xerr=err, fmt=".")