Plot a Vertical line in Matplotlib
Matplotlib is a popular python library used for plotting, It provides an object-oriented API to render GUI plots. Plotting a horizontal line is fairly simple, The following code shows how it can be done.
Making a single vertical line
Method #1: Using axvline()
This function adds the vertical lines across the axes of the plot
Syntax: matplotlib.pyplot.axvline(x, color, xmin, xmax, linestyle)
Parameters:
- x: Position on X axis to plot the line, It accepts integers.
- xmin and xmax: scalar, optional, default: 0/1. It plots the line in the given range
- color: color for the line, It accepts a string. eg ‘r’ or ‘b’ .
- linestyle: Specifies the type of line, It accepts a string. eg ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’
Python3
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize = ( 10 , 5 ))
plt.axvline(x = 7 , color = 'b' , label = 'axvline - full height' )
plt.show()
|
Output:
Method #2: Using vlines()
matplotlib.pyplot.vlines() is a function used in the plotting of a dataset. In matplotlib.pyplot.vlines(), vlines is the abbreviation for vertical lines. What this function does is very much clear from the expanded form, which says that function deals with the plotting of the vertical lines across the axes.
Syntax: vlines(x, ymin, ymax, colors, linestyles)
Parameters:
- x: Position on X axis to plot the line, It accepts integers.
- xmin and xmax: scalar, optional, default: 0/1. It plots the line in the given range
- color: color for the line, It accepts a string. eg ‘r’ or ‘b’ .
- linestyle: Specifies the type of line, It accepts a string. eg ‘-‘, ‘–‘, ‘-.’, ‘:’, ‘None’, ‘ ‘, ”, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’
Python3
import matplotlib.pyplot as plt
import numpy as np
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.show()
|
Output:
Method #3: Using plot()
The plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y.
Syntax : plot(x_points, y_points, scaley = False)
Parameters:
- x_points/y_points: points to plot
- scalex/scaley: Bool, These parameters determine if the view limits are adapted to the data limits
Python3
import matplotlib.pyplot as plt
plt.figure(figsize = ( 10 , 5 ))
plt.plot(( 0 , 0 ), ( 0 , 1 ), scaley = False )
plt.show()
|
Output:
Plotting multiple lines with the legend
The below methods can be used for plotting multiple lines in Python.
Method #1: Using axvline()
Python3
import matplotlib.pyplot as plt
import numpy as np
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 - % of full height' )
plt.legend(bbox_to_anchor = ( 1.0 , 1 ), loc = 'upper left' )
plt.show()
|
Output:
Method #2: Using vlines()
Python3
import matplotlib.pyplot as plt
import numpy as np
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(bbox_to_anchor = ( 1.0 , 1 ), loc = 'up' )
plt.show()
|
Output:
Last Updated :
13 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...