Table of Contents
Drawing a vertical line in Matplotlib is a simple and straightforward process that can be achieved using the “axvline” function. This function allows you to specify the x-coordinate at which you want the line to be drawn, along with other customizable parameters such as line color, style, and width. By using this function, you can easily add vertical lines to your plots and graphs, making it an essential tool for data visualization and analysis. With its user-friendly syntax and versatile options, Matplotlib’s “axvline” function provides a convenient solution for drawing vertical lines in your plots.
Draw a Vertical Line in Matplotlib (With Examples)
You can use the following syntax to draw a vertical line in Matplotlib:
import matplotlib.pyplotas plt #draw vertical line at x=2 plt.axvline(x=2)
The following examples show how to use this syntax in practice with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [5, 7, 8, 15, 26, 39, 45, 40]})
#view DataFrame
df
x y
0 1 5
1 2 7
2 3 8
3 4 15
4 5 26
5 6 39
6 7 45
7 8 40
Example 1: Draw One Vertical Line
The following code shows how to draw one vertical line on a Matplotlib plot:
import matplotlib.pyplotas plt
#create line plot
plt.plot(df.x, df.y)
#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--')
Example 2: Draw Multiple Vertical Lines
The following code shows how to draw multiple vertical lines on a Matplotlib plot:
import matplotlib.pyplotas plt
#create line plot
plt.plot(df.x, df.y)
#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--')
#add vertical line at x=4
plt.axvline(x=4, color='black', linestyle='-')

Example 3: Draw Multiple Vertical Lines with Legend
The following code shows how to draw multiple vertical lines on a Matplotlib plot and add a legend to make the lines easier to interpret:
import matplotlib.pyplotas plt
#create line plot
plt.plot(df.x, df.y)
#add vertical line at x=2
plt.axvline(x=2, color='red', linestyle='--', label='First Line')
#add vertical line at x=4
plt.axvline(x=4, color='black', linestyle='-', label='Second Line')
#add legend
plt.legend()
Note: Refer to the for a list of potential colors and linestyles you can apply to vertical lines.
Cite this article
stats writer (2024). How can I draw a vertical line in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-draw-a-vertical-line-in-matplotlib/
stats writer. "How can I draw a vertical line in Matplotlib?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-draw-a-vertical-line-in-matplotlib/.
stats writer. "How can I draw a vertical line in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-draw-a-vertical-line-in-matplotlib/.
stats writer (2024) 'How can I draw a vertical line in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-draw-a-vertical-line-in-matplotlib/.
[1] stats writer, "How can I draw a vertical line in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I draw a vertical line in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
