October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Add a Vertical Line in Matplotlib: Choose the Right Method

Use axvline for a full-height or Axes-fraction marker, and vlines when the segment endpoints are y data values.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use ax.axvline(x=...) for a vertical reference line that spans all or part of an Axes. If the line should run only between two y values in your data, use ax.vlines(...) instead.

Draw a full-height vertical reference line

Call ax.axvline() on the Axes where the line belongs. The x position is in data coordinates, and the line spans the Axes vertically by default.

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [2, 1, 3])
ax.axvline(x=1, color="red", linestyle="--", linewidth=1.5)
plt.show()

This adds a dashed red marker at x=1. The official Matplotlib axvline API documents the method and its styling properties. Although plt.axvline(...) is also available, ax.axvline(...) makes the target Axes explicit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set the line’s height as a fraction of the Axes

The ymin and ymax arguments to ax.axvline() are Axes fractions, not y data values: 0 is the bottom and 1 is the top. For example, this draws the line through the middle half of the Axes, regardless of the plotted y limits:

ax.axvline(x=1, ymin=0.25, ymax=0.75)

Draw a segment between y data values

Use ax.vlines() when the endpoints should be specific y values in the data coordinate system. Unlike axvline(), its ymin and ymax arguments are y data values.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [2, 5, 3])
ax.vlines(x=1, ymin=2, ymax=5, color="black", linestyle=":")
plt.show()

Here the dotted segment at x=1 runs from y=2 to y=5. vlines() also accepts array-like x positions and endpoints, so it can draw multiple vertical lines. Its API and return type are described in the Matplotlib Axes.vlines reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Style or move the line

Pass ordinary line properties such as color, linestyle, and linewidth to axvline() or vlines(). For instance, color="grey" creates a subdued reference marker; the Matplotlib infinite-lines gallery shows a styled reference-line example.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

axvline() returns a Line2D. To move a line after creating it, keep that object and set both x endpoints to the new x value:

line = ax.axvline(x=1)
line.set_xdata([3, 3])

The endpoints share the same x coordinate, which keeps the line vertical.

Which method should you use?

  • Use ax.axvline(x=...) for a full-height reference marker.
  • Use ax.axvline(x=..., ymin=..., ymax=...) when the vertical extent is a fraction of the Axes height.
  • Use ax.vlines(x=..., ymin=..., ymax=...) when the endpoints are y values in your data.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.