I’ve worked on countless data visualization projects. One thing I’ve learned is that showing just the data points often isn’t enough. You need to communicate the uncertainty or variability in your data. That’s where error bars come in.
Matplotlib, the go-to plotting library in Python, offers useful tools to add error bars to your plots.
In this article, I’ll walk you through different ways to plot error bars in Matplotlib. I’ll share practical tips and examples from my experience to help you create insightful visualizations that speak volumes.
What Are Error Bars and Why Use Them?
Error bars are graphical representations of the variability or uncertainty of data points. They indicate the range within which the true value likely falls, based on standard deviation, standard error, confidence intervals, or any other error metric.
For example, when plotting average monthly sales figures for a retail chain in New York, error bars can show the variation in sales across different stores. This helps stakeholders understand not just the average but also the spread and reliability of the data.
Method 1: Use plt.errorbar() for Basic Error Bars
The simplest way to add error bars in Matplotlib is with the plt.errorbar() function. It’s easy and flexible.
Example: Monthly Sales with Standard Deviation Error Bars
import matplotlib.pyplot as plt
import numpy as np
# Sample data: Average monthly sales in thousands for a retail chain in NY
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [50, 55, 53, 60, 62, 58]
# Assume these are standard deviations from multiple store sales
sales_std = [5, 3, 4, 6, 2, 5]
plt.figure(figsize=(8,5))
plt.errorbar(months, sales, yerr=sales_std, fmt='o', ecolor='red', capsize=5, capthick=2)
plt.title('Monthly Sales with Standard Deviation Error Bars')
plt.xlabel('Month')
plt.ylabel('Sales (Thousands)')
plt.grid(True)
plt.show()You can see the output in the screenshot below.

Explanation:
yerrspecifies the vertical error bar lengths (standard deviation in this case).fmt='o'plots data points as circles.ecolorsets the error bar color.capsizeandcapthickcontrol the appearance of error bar caps.
This method is great for quick visualizations and works well for both small and large datasets.
Read Matplotlib Unknown Projection ‘3d’
Method 2: Horizontal and Vertical Error Bars with plt.errorbar()
Sometimes, you may want to show uncertainty in both X and Y directions, especially when both measurements have errors.
Example: Compare Advertising Spend vs. Sales with Errors
import matplotlib.pyplot as plt
import numpy as np
# Advertising spend in thousands
ad_spend = [20, 25, 30, 35, 40, 45]
# Corresponding sales in thousands
sales = [55, 60, 65, 70, 72, 75]
# Errors in ad spend and sales (standard error)
ad_spend_err = [2, 1.5, 3, 2.5, 2, 3]
sales_err = [5, 4, 3, 5, 4, 6]
plt.figure(figsize=(8,5))
plt.errorbar(ad_spend, sales, xerr=ad_spend_err, yerr=sales_err, fmt='s', ecolor='green', capsize=4)
plt.title('Advertising Spend vs Sales with Error Bars')
plt.xlabel('Advertising Spend (Thousands)')
plt.ylabel('Sales (Thousands)')
plt.grid(True)
plt.show()You can see the output in the screenshot below.

Explanation:
Here, xerr and yerr represent the horizontal and vertical error bars, respectively. This dual-error bar approach is useful in regression analysis or when both variables have measurement uncertainties.
Check out Matplotlib 2d Surface Plot
Method 3: Use Asymmetric Error Bars
In many real-world scenarios, the upper and lower errors are not the same. Matplotlib allows you to specify asymmetric error bars by passing arrays or lists for yerr or xerr.
Example: Quarterly Revenue with Asymmetric Confidence Intervals
import matplotlib.pyplot as plt
import numpy as np
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
revenue = [200, 220, 210, 230]
# Lower and upper errors (e.g., 95% confidence intervals)
lower_errors = [15, 10, 20, 12]
upper_errors = [20, 15, 25, 18]
asymmetric_error = [lower_errors, upper_errors]
plt.figure(figsize=(8,5))
plt.errorbar(quarters, revenue, yerr=asymmetric_error, fmt='d', ecolor='blue', capsize=6)
plt.title('Quarterly Revenue with Asymmetric Error Bars')
plt.xlabel('Quarter')
plt.ylabel('Revenue (Thousands)')
plt.grid(True)
plt.show()You can see the output in the screenshot below.

Explanation:
yerrtakes a list of two lists: lower errors and upper errors.- This gives you flexibility to represent skewed data distributions or asymmetric confidence intervals.
Read Matplotlib is currently using agg, a non-GUI backend
Method 4: Customize Error Bars Appearance
From personal experience, customizing error bars improves readability, especially when presenting to stakeholders.
You can adjust:
- Color (
ecolor) - Line width (
elinewidth) - Cap size (
capsize) - Marker style (
fmt) - Alpha (transparency)
Example: Customized Error Bars for U.S. State Sales Data
import matplotlib.pyplot as plt
import numpy as np
states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
sales = [120, 95, 85, 110, 90]
errors = [10, 8, 7, 9, 6]
plt.figure(figsize=(10,6))
plt.errorbar(states, sales, yerr=errors, fmt='o', ecolor='purple', elinewidth=3, capsize=10, alpha=0.7)
plt.title('Sales by State with Customized Error Bars')
plt.xlabel('State')
plt.ylabel('Sales (Thousands)')
plt.grid(axis='y')
plt.show()This level of customization helps highlight important data points and makes the chart more visually appealing.
Check out Matplotlib Time Series Plot
Method 5: Add Error Bars to Bar Charts
Error bars aren’t limited to line or scatter plots. You can add them to bar charts using Matplotlib’s bar() method with the yerr parameter.
Example: Average Customer Ratings with Error Bars
import matplotlib.pyplot as plt
import numpy as np
products = ['Product A', 'Product B', 'Product C', 'Product D']
ratings = [4.2, 3.8, 4.5, 4.0]
rating_errors = [0.3, 0.2, 0.4, 0.25]
plt.figure(figsize=(8,5))
bars = plt.bar(products, ratings, yerr=rating_errors, capsize=8, color='skyblue', edgecolor='black')
plt.title('Average Customer Ratings with Error Bars')
plt.ylabel('Rating (out of 5)')
plt.ylim(0, 5.5)
plt.show()This is particularly useful when you want to show variability in survey results or product performance metrics.
Read Module ‘matplotlib’ has no attribute ‘artist’
Tips From My Experience
- Always label your error bars in the legend or caption to clarify what the error represents (standard deviation, standard error, confidence interval).
- Choose appropriate cap sizes; too small and they’re hard to see, too large and they clutter the plot.
- Use color wisely to distinguish error bars from data points. Avoid colors that blend with the background or markers.
- Keep your plots simple. Don’t overload with too many error bars if it makes the chart confusing. Sometimes multiple smaller charts are better.
- Test your plots on different screen sizes to ensure readability, especially if sharing with non-technical audiences.
Error bars are a simple yet powerful way to add depth to your data visualizations. Whether you’re presenting quarterly sales, survey results, or experimental data, showing uncertainty builds trust and clarity.
Matplotlib’s flexibility lets you create basic to highly customized error bars with ease. I encourage you to experiment with these methods and adapt them to your datasets.
You may like to read:
- Matplotlib xlim
- Module ‘matplotlib’ has no attribute ‘plot’
- Matplotlib Set y Axis Range
- Change the Pie Chart Title Font Size in Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.