Adding Vertical Line in a Python Plot

In this article, we are going to learn how to add a vertical line in matplotlib figures (Python plot)?
Submitted by Anuj Singh, on July 30, 2020

A vertical line is required for marking the extreme range or something related to the boundary limit. In some cases, it is also used for defining outliers, and therefore, it turns out to be a good technique in data visualization, and therefore, matplotlib has an inbuilt defined function for our operation, matplotlib.pyplot.axvline().

The following example illustrates all four possible examples.

Adding Vertical Line in a Python Plot (1)

Adding Vertical Line in a Python Plot (2)

Adding Vertical Line in a Python Plot (3)

Adding Vertical Line in a Python Plot (4)

Python code for adding vertical line in a plot

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0.1, 5, 0.1)
y = np.tan(x)*np.exp(-x)

plt.figure()
plt.plot(x,y, '.-', color='purple')
plt.axvline(x=3)
plt.title(' Vertical Line')
plt.show()

plt.figure()
plt.plot(x,y, '.-', color='purple')
plt.axvline(x=3, linewidth=3.0)
plt.title(' Vertical Line')
plt.show()

plt.figure()
plt.plot(x,y, '.-', color='purple')
plt.axvline(x=3, linewidth=2.0, color='grey')
plt.title(' Vertical Line')
plt.show()

plt.figure()
plt.plot(x,y, '.-', color='purple')
plt.axvline(x=2, color='grey')
plt.axvline(x=3, color='grey')
plt.title(' Vertical Line')
plt.show()

Output:

Output is as figure





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.