Remove a Horizontal Line in Matplotlib using Python

When I first started working with Matplotlib in Python, I often added extra lines to my plots to highlight thresholds or averages. But sometimes, I needed to remove those horizontal lines later.

At first, I thought there would be a direct to button “remove line”, but Matplotlib doesn’t work that way. Instead, you need to handle the line objects directly.

In this tutorial, I’ll walk you through different methods I use to remove horizontal lines in Matplotlib. I’ll explain each method with simple examples, and I’ll also share real-life cases where I applied them in my Python projects here in the USA.

Remove Horizontal Lines in Matplotlib

Sometimes, horizontal lines are useful when you want to show a benchmark. For example, in a sales report, you might add a line at $10,000 to show the target revenue.

But when you present the same chart to a different audience, that line may not be relevant anymore. That’s when removing it becomes necessary.

I’ve also seen situations where multiple horizontal lines clutter the chart, making it harder to understand. In those cases, removing some lines keeps the chart clean and professional.

Method 1 – Remove a Horizontal Line Using remove()

The simplest way to remove a horizontal line in Matplotlib is by calling the remove() method on the line object.

I’ll show you an example where I plot sales data for a small retail store in New York. I add a horizontal line for the target sales, and then I remove it.

import matplotlib.pyplot as plt

# Sample sales data for a US retail store
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [8000, 9500, 10500, 9800, 11000, 12000]

# Create the plot
fig, ax = plt.subplots()
ax.plot(months, sales, marker='o', label="Monthly Sales")

# Add a horizontal line for the target
target_line = ax.axhline(y=10000, color='red', linestyle='--', label="Target")

# Now remove the horizontal line
target_line.remove()

# Final chart
ax.set_title("Monthly Sales Performance")
ax.set_ylabel("Sales in USD")
ax.legend()
plt.show()

You can refer to the screenshot below to see the output.

Remove a Horizontal Line in Matplotlib Python

In this code, I first add the horizontal line with axhline. Then, I call target_line.remove() to remove it.

Method 2 – Remove Multiple Horizontal Lines with a Loop

Sometimes, you may have multiple horizontal lines in the same chart. For example, I once worked on a project where I added lines for different sales thresholds: minimum, target, and maximum.

import matplotlib.pyplot as plt

# Sample sales data for a California store
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [7000, 8200, 9100, 8800, 9700, 10200]

fig, ax = plt.subplots()
ax.plot(months, sales, marker='o', label="Monthly Sales")

# Add multiple horizontal lines
lines = []
lines.append(ax.axhline(y=8000, color='green', linestyle='--', label="Minimum"))
lines.append(ax.axhline(y=10000, color='red', linestyle='--', label="Target"))
lines.append(ax.axhline(y=12000, color='blue', linestyle='--', label="Maximum"))

# Remove all horizontal lines
for line in lines:
    line.remove()

ax.set_title("Sales Performance with Removed Thresholds")
ax.set_ylabel("Sales in USD")
ax.legend()
plt.show()

You can refer to the screenshot below to see the output.

Remove a Horizontal Line in Matplotlib using Python

In this approach, I store all the line objects in a list. Later, I loop through that list and remove each one.

Method 3 – Remove Horizontal Lines by Accessing ax.lines

Another trick I use is to access the ax.lines list directly. Every line you add to a Matplotlib axis is stored there.

This method is useful when you don’t have the line object saved in a variable.

import matplotlib.pyplot as plt

# Sample data for a Chicago store
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [6000, 7500, 8200, 9100, 9700, 10300]

fig, ax = plt.subplots()
ax.plot(months, sales, marker='o', label="Monthly Sales")

# Add a horizontal line
ax.axhline(y=9000, color='purple', linestyle='--', label="Benchmark")

# Remove the last added line using ax.lines
ax.lines[-1].remove()

ax.set_title("Chicago Store Sales")
ax.set_ylabel("Sales in USD")
ax.legend()
plt.show()

You can refer to the screenshot below to see the output.

Remove a Horizontal Line in Python Matplotlib

This works well when you know the order of the lines, but it can get tricky if you have many lines and don’t remember which one is where.

Method 4 – Conditional Removal of Horizontal Lines

Sometimes, you may want to remove only specific lines based on their properties, like color or style.

For example, I once had a chart with multiple benchmarks, but I only wanted to remove the red dashed line.

Here’s how I handled that in Python:

import matplotlib.pyplot as plt

# Sample data for a Texas store
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [8500, 8900, 9400, 9700, 10100, 10800]

fig, ax = plt.subplots()
ax.plot(months, sales, marker='o', label="Monthly Sales")

# Add multiple horizontal lines
ax.axhline(y=9000, color='green', linestyle='--', label="Minimum")
ax.axhline(y=10000, color='red', linestyle='--', label="Target")
ax.axhline(y=11000, color='blue', linestyle='--', label="Maximum")

# Remove only the red line
for line in ax.lines:
    if line.get_color() == 'red':
        line.remove()

ax.set_title("Texas Store Sales with Selective Line Removal")
ax.set_ylabel("Sales in USD")
ax.legend()
plt.show()

This method is very powerful because it lets you fine-tune which horizontal lines you want to keep and which ones to remove.

Tips for Working with Horizontal Lines in Python Matplotlib

While removing horizontal lines is easy once you know the methods, here are a few tips I’ve learned over the years:

  • Always store line objects in variables if you plan to remove them later.
  • Use conditional checks when you have many lines with different styles.
  • Remember that removing a line does not affect the data—it only changes the visualization.
  • If you are working with dynamic charts (like in dashboards), make sure to refresh the plot after removing lines.

Conclusion

Removing horizontal lines in Matplotlib using Python is not difficult once you understand how line objects work. You can use Python’s remove() method for single lines, loops for multiple lines, or even conditional checks for selective removal.

I’ve personally used all these methods in real-world projects here in the USA, from sales dashboards to data analysis reports.

I hope you found this guide helpful. Try these methods in your own Python projects, and you’ll see how flexible Matplotlib can be when it comes to customizing your charts.

You may also like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.