How to Draw a Vertical Line in Matplotlib?

As a Python developer with over a decade of experience, I’ve found Matplotlib to be an invaluable tool for data visualization.

In this guide, I’ll walk you through simple and practical ways to draw vertical lines in Matplotlib. The methods I share come from real-world experience, designed to help you quickly add vertical markers to your plots without fuss.

Let’s get in!

Methods to Draw Vertical Lines in Your Plots

Let me explain to you the methods to draw vertical lines in your plots.

Method 1: Use plt.axvline() to Draw a Simple Vertical Line

The easiest way to add a vertical line is by using Matplotlib’s axvline() function in Python. This method lets you draw a vertical line at a specific x-coordinate across the entire y-axis.

Here’s how I use it in practice:

import matplotlib.pyplot as plt

# Sample monthly sales data (in thousands) for a US retailer
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 135, 150, 145, 160, 170]

plt.plot(months, sales, marker='o')

# Draw a vertical line to mark the start of a summer sale in April
plt.axvline(x=3, color='red', linestyle='--', linewidth=2, label='Summer Sale Start')

plt.title('Monthly Sales with Summer Sale Highlight')
plt.xlabel('Month')
plt.ylabel('Sales (in thousands)')
plt.legend()
plt.show()

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

matplotlib vertical line

In this example, x=3 corresponds to the fourth month (April), since Python indexing starts at zero. The vertical red dashed line marks the summer sale.

Why I Like axvline()

  • Simple and quick to use.
  • Customizable color, line style, and width.
  • Automatically spans the full height of the plot.

Read the Matplotlib Pie Chart Tutorial

Method 2: Use plt.vlines() for Multiple Vertical Lines

If you want to add multiple vertical lines at once, vlines() is a handy function. It allows you to specify multiple x-coordinates and the y-range for each line.

Here’s a scenario where I used vlines() to mark quarterly review dates on a sales trend:

import matplotlib.pyplot as plt

months = range(1, 13)
sales = [100, 110, 130, 150, 160, 170, 180, 175, 165, 160, 150, 140]

plt.plot(months, sales, marker='o')

# Quarterly review months: March, June, September, December
quarterly_months = [3, 6, 9, 12]

# Draw vertical lines from y=0 to y=max sales value
plt.vlines(x=quarterly_months, ymin=0, ymax=max(sales), colors='green', linestyles='dotted', label='Quarterly Reviews')

plt.title('Annual Sales with Quarterly Reviews')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.show()

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

plot vertical line matplotlib

This method gives you precise control over the length of each vertical line, which can be useful if you want to highlight a specific range rather than spanning the whole plot.

Check out Matplotlib Scatter Plot Color

Method 3: Use ax.plot() with Vertical Lines

Sometimes I prefer to use the plot() function directly to draw vertical lines by specifying the same x-coordinate twice with different y-values. This approach is flexible when you want to add vertical lines within subplots or customized axes.

Example:

fig, ax = plt.subplots()

months = range(1, 7)
sales = [120, 135, 150, 145, 160, 170]

ax.plot(months, sales, marker='o')

# Draw a vertical line at month 4 (April) from y=100 to y=170
ax.plot([4, 4], [100, 170], color='purple', linewidth=2, label='Special Event')

ax.set_title('Sales Data with Event Marker')
ax.set_xlabel('Month')
ax.set_ylabel('Sales')
ax.legend()
plt.show()

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

vertical line matplotlib

This method is especially useful when working with multiple plots or customizing axes properties.

Read Matplotlib fill_between

Tips for Using Vertical Lines Effectively

  • Label your lines: Use the label parameter and add a legend to make your vertical lines meaningful.
  • Choose contrasting colors: Make sure the vertical line stands out but doesn’t overwhelm the plot.
  • Use line styles: Dashed or dotted lines often work better for markers to avoid clutter.
  • Match x-coordinates carefully: When using categorical x-axis labels (like months), remember to use the correct index or convert labels to numerical positions.

Drawing vertical lines in Matplotlib is easy but powerful. Whether you’re highlighting key dates in a sales report or marking thresholds in financial data, vertical lines help make your visualizations clearer and more impactful.

I use plt.axvline() for quick markers and plt.vlines() when dealing with multiple lines. For more customized plotting, ax.plot() gives me the flexibility I need.

Try these methods in your next data project and see how vertical lines can enhance your charts!

This tutorial helps you add vertical lines to your plots with ease, making your Python data visualizations more insightful and professional. Happy plotting!

You may also read other Matplotlib-related tutorials:

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.