Open In App

Matplotlib.axes.Axes.axvline() in Python

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.axvline() Function

The Axes.axvline() function in axes module of matplotlib library is used to add a vertical line across the axis.

Syntax: Axes.axvline(self, x=0, ymin=0, ymax=1, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x: This parameter is the x position in data coordinates of the vertical line with default value of 0.
  • ymin: This parameter should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.Its default value of 0.
  • ymax: This parameter should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot. Its default value of 1.

Returns: This returns the following:

  • lines:This returns the list of Line2D objects representing the plotted data.

Below examples illustrate the matplotlib.axes.Axes.axhline() function in matplotlib.axes:

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.collections as collections
  
t = np.arange(0.0, 5, 0.01)
s1 = np.sin(4 * np.pi * t)
  
fig, ax = plt.subplots()
  
ax.plot(t, s1, color ='black', alpha = 0.75, lw = 1)
ax.axvline(3, color ='green', lw = 2, alpha = 0.75)
ax.set_title('matplotlib.axes.Axes.axvline() Example')
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
t = np.linspace(-10, 10, 100)
sig = 1 / t**2
fig, ax = plt.subplots()
  
plt.axvline(color ="green", alpha = 0.8, lw = 1.5)
plt.plot(t, sig, linewidth = 1.5, color ="black",
         alpha = 0.6,
         label = r"$\sigma(t) = \frac{1}{x ^ 2}$")
  
plt.xlim(-10, 10)
plt.xlabel("t")
plt.legend(fontsize = 14)
  
ax.set_title('matplotlib.axes.Axes.axvline() Example')
plt.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads