Plot a Vertical line in Matplotlib
Last Updated :
26 Apr, 2025
Plotting vertical lines is a technique in data visualization which is used to highlight specific data points, thresholds or regions on a graph. Whether we're marking key values, dividing data into segment, vertical lines can make our charts more informative. In this article, we'll see different methods to plot vertical lines in Matplotlib.
Method 1: Using axline()
The axvline() function adds a vertical line at a specified x-coordinate across entire height of the plot. It is one of the simplest and most commonly used methods for drawing vertical lines.
Syntax: matplotlib.pyplot.axvline(x, color, xmin, xmax, linestyle)
Parameters:
- x: The x-coordinate where the vertical line will be drawn.
- xmin and xmax: Define vertical range (0 to 1). Defaults are 0 and 1 covering full height.
- color: Color for the line (e.g 'r' or 'b')
- linestyle: Defines style of the line (e.g
'-' or
'--'
)
Example 1: Plotting a Single Vertical Line
We will be using Matplotlib library for this.
Python
import matplotlib.pyplot as plt
plt.figure(figsize = (10, 5))
plt.axvline(x = 7, color = 'b', label = 'axvline - full height')
plt.legend()
plt.show()
Output:
Single Line by using axvline()Example 2: Plotting Multiple Vertical Lines
For plotting multiple vertical lines we can use axvline() multiple times with different x
positions.
Python
import matplotlib.pyplot as plt
plt.figure(figsize = (10, 5))
plt.axvline(x = 7, color = 'b', label = 'axvline - full height')
plt.axvline(x = 7.25, ymin = 0.1, ymax = 0.90, color = 'r',
label = 'axvline - partial height')
plt.legend()
plt.show()
Output:
Multiple Vertical Lines by using axvline()Method 2: Using vlines()
The vlines() function is also used for plotting vertical lines when we need to specify both ymin and ymax which are the start and end points on the y-axis.
Syntax: matplotlib.pyplot.vlines(x, ymin, ymax, colors, linestyles)
Parameters:
- x: The x-coordinate where vertical line will be drawn.
- ymin, ymax: Starting and ending points of the line on y-axis.
- colors: Color of the line(eg purple or green)
- linestyles: Style of the line (e.g
'-'
, '--'
).
Example 1: Plotting a Vertical Line with Specific y-limits
Python
import matplotlib.pyplot as plt
xs = [1, 100]
plt.figure(figsize = (10, 7))
plt.vlines(x = 37, ymin = 0, ymax = max(xs), colors = 'purple', label = 'vline_multiple - full height')
plt.legend()
plt.show()
Output:
Single Line by using vlines()Example 2: Plotting Multiple Vertical Lines
We can plot multiple vertical lines by passing a list of x
positions to vlines(). We can also change the yminand ymaxfor each line.
Python
import matplotlib.pyplot as plt
xs = [1, 100]
plt.figure(figsize = (10, 7))
plt.vlines(x = [37, 37.25, 37.5], ymin = 0, ymax = max(xs),
colors = 'purple',
label = 'vline_multiple - full height')
plt.vlines(x = [38, 38.25, 38.5], ymin = [0, 25, 75], ymax = max(xs),colors = 'teal', label = 'vline_multiple - partial height')
plt.vlines(x = 39, ymin = 0, ymax = max(xs), colors = 'green', label = 'vline_single - full height')
plt.vlines(x = 39.25, ymin = 25, ymax = max(xs), colors = 'green', label = 'vline_single - partial height')
plt.legend()
plt.show()
Output:
Multiple Line by using vlines()Method 3: Using plot() for a Vertical Line
The plot() function can also be used to create a vertical line by plotting two points along the y-axis at same x-coordinate. This method is less common but still useful for simple vertical lines.
Syntax : matplotlib.pyplot.plot(x_points, y_points, scaley=False)
Parameters:
- x_points, y_points: Points for plotting the line. For a vertical line set the x-coordinates same.
- scaley: If True the plot's y-axis limits are adjusted to fit the line.
Python
import matplotlib.pyplot as plt
plt.figure(figsize = (10, 5))
plt.plot((0, 0), (0, 1), scaley = False)
plt.show()
Output:
Single Line by using plot()With these methods for plotting vertical lines in Matplotlib we have flexibility to enhance our plots and highlight important points in a way that best suits our data visualization needs.