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.
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:
#1 Best Overall
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.
Rank #2
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.
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.
Quick Recap
Best Value
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.




