How to Make a Dashed Horizontal Line in Python Matplotlib

When I started working with Python data visualization, I often needed to highlight specific thresholds in my plots.

Sometimes, a horizontal line was the best way to do it. But instead of a solid line, I wanted a dashed horizontal line to make the chart more readable.

At first, it wasn’t obvious how to do this in Matplotlib. But after experimenting with different methods, I found several simple ways to draw dashed horizontal lines in Python.

In this tutorial, I’ll show you step-by-step how I do it. I’ll cover different methods so you can choose the one that works best for your project.

Methods to Make a Dashed Horizontal Line in Python Matplotlib

A dashed horizontal line is useful when you want to:

  • Highlight a target value (like a sales goal in USD).
  • Show a threshold (like a passing score in an exam).
  • Mark a zero baseline in financial charts.
  • Separate sections in a graph visually without making the line too bold.

As a Python developer with more than 10 years of experience, I can say that dashed lines are one of the simplest but most effective ways to improve chart readability.

Method 1 – Use axhline() in Matplotlib

The easiest way to draw a dashed horizontal line in Python Matplotlib is by using the axhline() function. This function lets you specify the y-position and style of the line.

Here’s the full Python code:

import matplotlib.pyplot as plt

# Sample data: Average monthly electricity bills in USD
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
bills = [120, 135, 150, 160, 155, 170]

# Create the plot
plt.plot(months, bills, marker="o", label="Monthly Bills")

# Add a dashed horizontal line at $150 (budget threshold)
plt.axhline(y=150, color="red", linestyle="--", linewidth=2, label="Budget Limit")

# Add labels and title
plt.title("Average Monthly Electricity Bills in the USA")
plt.xlabel("Month")
plt.ylabel("Bill Amount (USD)")
plt.legend()

# Show the plot
plt.show()

You can see the output in the screenshot below.

How to Make a Dashed Horizontal Line in Python Matplotlib

This code plots monthly electricity bills and adds a dashed horizontal line at $150. The linestyle=”–” makes the line dashed, and linewidth=2 makes it thicker for better visibility.

Method 2 – Use hlines() in Matplotlib

Another way to create a dashed horizontal line in Python is with the hlines() function. This method is useful when you want to control the start and end points of the line.

Here’s an example:

import matplotlib.pyplot as plt

# Sample data: Average daily temperatures in New York (°F)
days = list(range(1, 11))
temps = [72, 74, 76, 78, 80, 82, 79, 77, 75, 73]

# Create the plot
plt.plot(days, temps, marker="o", label="Daily Temp")

# Add a dashed horizontal line at 78°F
plt.hlines(y=78, xmin=1, xmax=10, colors="blue", linestyles="--", linewidth=2, label="Comfort Level")

# Add labels and title
plt.title("Daily Temperatures in New York")
plt.xlabel("Day")
plt.ylabel("Temperature (°F)")
plt.legend()

# Show the plot
plt.show()

You can see the output in the screenshot below.

Make a Dashed Horizontal Line in Python Matplotlib

Here, the dashed horizontal line is drawn only from day 1 to day 10. This gives you more flexibility compared to axhline().

Method 3 – Use plot() with constant y-values

Sometimes I prefer to use the regular plot() function in Python Matplotlib to create a dashed line horizontally. This method is handy when I want full control over the line style.

Here’s how I do it:

import matplotlib.pyplot as plt

# Sample data: Stock prices of a company (USD)
days = [1, 2, 3, 4, 5]
prices = [210, 220, 215, 225, 230]

# Create the plot
plt.plot(days, prices, marker="o", label="Stock Price")

# Add a dashed horizontal line at $220
plt.plot([1, 5], [220, 220], linestyle="--", color="green", linewidth=2, label="Target Price")

# Add labels and title
plt.title("Stock Prices of a Tech Company (USD)")
plt.xlabel("Day")
plt.ylabel("Price (USD)")
plt.legend()

# Show the plot
plt.show()

You can see the output in the screenshot below.

Python Matplotlib Make a Dashed Horizontal Line

In this case, I manually set the x-range [1, 5] and kept the y-value constant at 220 to draw the dashed line. This method gives me maximum customization.

Customize dashed horizontal lines in Python

Once you know how to draw a dashed horizontal line, you can customize it further.

Here are some useful options:

  • Change dash style: linestyle=”–“, linestyle=”-.”, or linestyle=”:”.
  • Change color: Use names like “red”, “blue”, or hex codes like “#FF5733”.
  • Change thickness: Use linewidth=1.5 or linewidth=3 for thicker lines.
  • Add transparency: Use alpha=0.5 to make the line semi-transparent.

Example with custom dash style:

import matplotlib.pyplot as plt

# Create a simple plot
plt.plot([0, 10], [0, 10], label="Data")

# Add a custom dashed horizontal line
plt.axhline(y=5, color="purple", linestyle=(0, (5, 10)), linewidth=2, label="Custom Dash")

plt.title("Custom Dashed Horizontal Line in Python Matplotlib")
plt.legend()
plt.show()

Here, linestyle=(0, (5, 10)) creates a custom dash pattern (5 pixels on, 10 pixels off).

Real-world example: Highlight passing scores in exams

Let’s take a practical example that many of us can relate to. Suppose we have exam scores of students in a U.S. high school, and we want to highlight the passing score (60).

Here’s how I would do it in Python:

import matplotlib.pyplot as plt

# Student names and scores
students = ["Alice", "Bob", "Charlie", "David", "Eva", "Frank"]
scores = [55, 65, 72, 48, 90, 60]

# Create the plot
plt.bar(students, scores, color="skyblue")

# Add a dashed horizontal line at passing score (60)
plt.axhline(y=60, color="red", linestyle="--", linewidth=2, label="Passing Score")

# Add labels and title
plt.title("High School Exam Scores (USA)")
plt.xlabel("Students")
plt.ylabel("Scores")
plt.legend()

# Show the plot
plt.show()

This makes it very easy to see who passed and who didn’t. The dashed horizontal line at 60 acts as a clear visual threshold.

Key takeaways

  • Use axhline() for quick full-width dashed horizontal lines.
  • Use hlines() when you want to control the start and end points.
  • Use plot() for maximum customization of dashed lines.
  • Always customize color, thickness, and dash style to match your chart’s needs.

When I first learned Python Matplotlib, I underestimated how powerful a simple dashed horizontal line could be. Now, I use them all the time to highlight thresholds, targets, and baselines in my charts.

If you’re working on data visualization in Python, I recommend trying out these methods in your next project. It will make your charts not only more professional but also easier to understand.

You may also 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.