Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Plot a Vertical line in Matplotlib
Python's Matplotlib library provides powerful tools for creating visual representations in the form of plots and graphs. One useful feature is plotting vertical lines to add reference lines or highlight specific points on plots. The built-in methods axvline(), vlines(), and plot() allow you to create vertical lines with customizable parameters such as position, color, and linestyle.
Using axvline() Method
The axvline() method is the simplest way to plot a vertical line in Matplotlib. It automatically spans the entire y-axis of the plot, making it ideal for reference lines.
Syntax
axvline(x=position, color='color', linestyle='style', alpha=transparency)
Here, position specifies the x-coordinate where the vertical line will be drawn.
Basic Example
Let's plot a simple vertical line at x=2 ?
import matplotlib.pyplot as plt
plt.axvline(x=2)
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid(True, alpha=0.3)
plt.title('Vertical Line using axvline()')
plt.show()
Customized Example
Now let's customize the line with color, style, and transparency ?
import matplotlib.pyplot as plt
plt.axvline(x=2, color='red', linestyle='--', alpha=0.7, linewidth=2)
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid(True, alpha=0.3)
plt.title('Customized Vertical Line')
plt.show()
Using plot() Method
The plot() method allows you to specify the exact y-range of the vertical line by providing two points with the same x-coordinate but different y-coordinates.
Syntax
plot([x1, x2], [y1, y2], color='color', linestyle='style')
Basic Example
Plot a vertical line from y=1 to y=4 at x=3 ?
import matplotlib.pyplot as plt
plt.plot([3, 3], [1, 4])
plt.xlim(0, 6)
plt.ylim(0, 5)
plt.grid(True, alpha=0.3)
plt.title('Vertical Line using plot()')
plt.show()
Customized Example
Add styling and markers to the vertical line ?
import matplotlib.pyplot as plt
plt.plot([3, 3], [1, 4], color='blue', linestyle=':', marker='o', markersize=8, linewidth=2)
plt.xlim(0, 6)
plt.ylim(0, 5)
plt.grid(True, alpha=0.3)
plt.title('Customized Vertical Line with Markers')
plt.show()
Using vlines() Method
The vlines() method provides the most control by allowing you to specify the x-coordinate and the exact y-range of the vertical line.
Syntax
vlines(x=position, ymin=bottom, ymax=top, colors='color', linestyles='style')
Basic Example
Create a vertical line at x=2.5 spanning from y=0.5 to y=3.5 ?
import matplotlib.pyplot as plt
plt.vlines(x=2.5, ymin=0.5, ymax=3.5)
plt.xlim(0, 5)
plt.ylim(0, 4)
plt.grid(True, alpha=0.3)
plt.title('Vertical Line using vlines()')
plt.show()
Customized Example
Apply custom styling to the vertical line ?
import matplotlib.pyplot as plt
plt.vlines(x=2.5, ymin=0.5, ymax=3.5, colors='green', linestyles='solid', linewidth=4)
plt.xlim(0, 5)
plt.ylim(0, 4)
plt.grid(True, alpha=0.3)
plt.title('Thick Green Vertical Line')
plt.show()
Comparison of Methods
| Method | Y-Range Control | Best For | Key Parameters |
|---|---|---|---|
axvline() |
Full axis span | Reference lines | x, color, linestyle, alpha |
plot() |
Custom range | Lines with markers | [x,x], [y1,y2], marker |
vlines() |
Precise control | Multiple lines | x, ymin, ymax, colors |
Conclusion
Use axvline() for simple reference lines that span the entire plot. Choose plot() when you need markers or custom y-ranges. Use vlines() for precise control over line positioning and when plotting multiple vertical lines.
