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.
Similar Reads
Line plot styles in Matplotlib
Line plots are important data visualization elements that can be used to identify relationships within the data. Using matplotlib.pyplot.plot() function we can plot line plots. Styling tools in this helps us customize line plots according to our requirements which helps in better representations. Li
3 min read
How to use matplotlib plot inline?
Matplotlib is a Python library that helps in drawing graphs. It is used in data visualization and graph plotting. Matplotlib Plot Inline is a package that supports Matplotlib to display plots directly inline and save them to notebooks. In this article, we'll cover the following:Â What is Matplotlib I
3 min read
How to plot a dashed line in matplotlib?
Matplotlib is used to create visualizations and plotting dashed lines is used to enhance the style and readability of graphs. A dashed line can represent trends, relationships or boundaries in data. Below we will explore how to plot and customize dashed lines using Matplotlib. To plot dashed line:Sy
2 min read
matplotlib.lines.VertexSelector class in Python
The matplotlib.lines.VertexSelector class belongs to the matplotlib.lines module. The matplotlib.lines module contains all the 2D line class that can be drawn with a variety of line styles, markers, and colors. matplotlib.lines.VertexSelector class is used to for managing the callbacks that maintain
2 min read
Plot a Horizontal line in Matplotlib
In Matplotlib, we can draw horizontal lines on a plot to indicate thresholds, reference points or important levels in the data. These lines can be used to highlight specific values for better visualization. We can create a single horizontal line, which is useful for marking a single reference level,
3 min read
Line chart in Matplotlib - Python
Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
Matplotlib.pyplot.ion() in Python
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. The matplotlib.pyplot.ion() is used to turn on interactive mode. To check the status of
4 min read
How to update a plot in Matplotlib?
In this article, let's discuss how to update a plot in Matplotlib. Updating a plot simply means plotting the data, then clearing the existing plot, and then again plotting the updated data and all these steps are performed in a loop. Functions Used:canvas.draw(): It is used to update a figure that
1 min read
How to Plot a Time Series in Matplotlib?
Time series data is the data marked by some time. Each point on the graph represents a measurement of both time and quantity. Â A time-series chart is also known as a fever chart when the data are connected in chronological order by a straight line that forms a succession of peaks and troughs. x-axis
4 min read
Bar Plot in Matplotlib
A bar plot uses rectangular bars to represent data categories, with bar length or height proportional to their values. It compares discrete categories, with one axis for categories and the other for values.Consider a simple example where we visualize the sales of different fruits:Pythonimport matplo
5 min read