Recently, I was working on a Python project where I had to visualize some experimental data with error bars and also emphasize a reference value using a horizontal line.
At first, I thought Matplotlib might have a built-in option for combining error bars with horizontal lines. But after a bit of exploration, I realized we need to combine plt.errorbar() with plt.axhline() or plt.hlines() to achieve this.
In this tutorial, I will show you how to create Matplotlib errorbar plots with horizontal lines in Python. I’ll walk you through multiple methods with complete Python code so you can adapt them to your own projects.
What is an Errorbar in Python Matplotlib?
An error bar in Python Matplotlib is a graphical representation of variability in data. It shows how uncertain or variable the measurements are.
When you use plt.errorbar() in Python, you can display vertical or horizontal error ranges around your data points. This is especially useful in scientific research, finance, or any domain where uncertainty matters.
Methods to Add a Horizontal Line to an Errorbar Plot
In many real-world cases, we need to compare our data against a constant benchmark or threshold. A horizontal line helps highlight this reference point clearly.
For example, if you are analyzing average daily sales of a product in the USA, you might want to show the national average sales as a horizontal line. This makes it easy to see which data points are above or below the benchmark.
Method 1 – Use plt.errorbar() with plt.axhline()
The first method is to use plt.errorbar() to plot the data with error bars and then add a horizontal line using plt.axhline().
This approach is simple and works for most cases where you want a straight horizontal line across the entire plot.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(1, 6)
y = np.array([10, 12, 9, 14, 13])
errors = np.array([1, 0.8, 1.2, 0.5, 1])
# Plot error bars
plt.errorbar(x, y, yerr=errors, fmt='o', capsize=5, label='Data with Error')
# Add horizontal line at y=12
plt.axhline(y=12, color='red', linestyle='--', label='Benchmark Line')
# Labels and title
plt.xlabel("Day")
plt.ylabel("Sales (in 1000s)")
plt.title("Sales Data with Error Bars and Benchmark Line")
plt.legend()
plt.show()You can see the output in the screenshot below.

In this Python example, I plotted sales data with error bars and then added a horizontal line at y=12. This method is useful when you want to emphasize a single benchmark value across the whole chart.
Method 2 – Use plt.errorbar() with plt.hlines()
Another way to add a horizontal line is to use plt.hlines(). Unlike plt.axhline(), this function lets you restrict the horizontal line to a specific range on the x-axis.
This is helpful when you only want the horizontal line to appear across a part of your errorbar plot.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(1, 8)
y = np.array([5, 7, 6, 8, 7, 9, 10])
errors = np.array([0.5, 0.7, 0.6, 0.8, 0.5, 0.9, 1])
# Plot error bars
plt.errorbar(x, y, yerr=errors, fmt='o', capsize=4, label='Measured Data')
# Add horizontal line between x=2 and x=6 at y=7.5
plt.hlines(y=7.5, xmin=2, xmax=6, colors='green', linestyles='dashed', label='Target Range')
# Labels and title
plt.xlabel("Week")
plt.ylabel("Production Output (tons)")
plt.title("Production Data with Error Bars and Target Line")
plt.legend()
plt.show()You can see the output in the screenshot below.

In this Python code, I added a horizontal line only between x=2 and x=6. This method is perfect when you want to highlight a specific time range or subset of your data.
Method 3 – Combine Multiple Horizontal Lines with Error Bars
Sometimes, you may want to add more than one horizontal line to your errorbar plot. This is common when you need to show multiple thresholds or tolerance bands.
In Python Matplotlib, you can simply call plt.axhline() or plt.hlines() multiple times to achieve this.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(1, 11)
y = np.array([15, 14, 16, 13, 17, 18, 16, 15, 19, 20])
errors = np.random.uniform(0.5, 1.5, size=10)
# Plot error bars
plt.errorbar(x, y, yerr=errors, fmt='o', capsize=3, label='Observed Data')
# Add multiple horizontal lines
plt.axhline(y=14, color='red', linestyle='--', label='Lower Threshold')
plt.axhline(y=18, color='blue', linestyle='-.', label='Upper Threshold')
# Labels and title
plt.xlabel("Experiment Number")
plt.ylabel("Measurement Value")
plt.title("Errorbar Plot with Multiple Threshold Lines")
plt.legend()
plt.show()You can see the output in the screenshot below.

In this Python example, I added two horizontal lines to represent lower and upper thresholds. This method is useful in quality control or scientific experiments where you need to check if values fall within acceptable limits.
Method 4 – Style Horizontal Lines in Errorbar Plots
Matplotlib allows you to customize the appearance of horizontal lines with color, style, and width. This makes your Python visualization more professional and easier to interpret.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(1, 6)
y = np.array([50, 55, 52, 58, 54])
errors = np.array([2, 3, 1.5, 2.5, 2])
# Plot error bars
plt.errorbar(x, y, yerr=errors, fmt='o', capsize=5, color='black', label='Survey Data')
# Add styled horizontal line
plt.axhline(y=55, color='orange', linestyle='dotted', linewidth=2, label='National Average')
# Labels and title
plt.xlabel("Region")
plt.ylabel("Survey Score")
plt.title("Survey Results with Error Bars and Styled Benchmark Line")
plt.legend()
plt.show()In this Python example, I used a dotted orange line with increased width to highlight the benchmark. This method is best when you want your horizontal line to stand out clearly from the errorbar data.
Method 5 – Add Horizontal Line with Annotations
Sometimes, you may want to label the horizontal line directly on the plot. This makes the visualization more self-explanatory without needing to rely only on the legend.
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(1, 9)
y = np.array([30, 32, 29, 35, 33, 31, 36, 34])
errors = np.array([1.5, 2, 1, 2.5, 1.8, 1.2, 2.1, 1.7])
# Plot error bars
plt.errorbar(x, y, yerr=errors, fmt='o', capsize=4, label='Measurements')
# Add horizontal line
plt.axhline(y=32, color='purple', linestyle='--')
# Add annotation
plt.text(8.1, 32, "Target = 32", color='purple', va='center')
# Labels and title
plt.xlabel("Sample ID")
plt.ylabel("Value")
plt.title("Errorbar Plot with Annotated Horizontal Line")
plt.legend()
plt.show()In this Python code, I placed an annotation at the end of the horizontal line to indicate the target value.
Best Practices for Using Errorbars with Horizontal Lines
- Always label the horizontal line in the legend or with annotations.
- Use different colors and styles to distinguish multiple horizontal lines.
- Keep error bars visible by choosing contrasting line styles for benchmarks.
- Avoid clutter by limiting the number of horizontal lines in one chart.
Adding a horizontal line to an errorbar plot in Python Matplotlib is a simple yet powerful way to highlight benchmarks, thresholds, or averages.
I showed you five methods: using plt.axhline(), plt.hlines(), multiple lines, styled lines, and annotated lines. Each method has its own advantage depending on your use case.
If you’re working with real-world datasets, whether it’s sales, surveys, or scientific experiments, combining error bars with horizontal lines can make your visualizations much clearer and more insightful.
You may also like to read other articles.
- Make a Dashed Horizontal Line in Python Matplotlib
- Add Horizontal Lines with Labels in Python Matplotlib
- Plot Multiple Horizontal Lines in Matplotlib using Python
- Save NumPy Array as PNG Image in Python 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.