Plot a Vertical line in Matplotlib - GeeksforGeeks

25 Aug, 2021
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.
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
# importing the modulesimport matplotlib.pyplot as pltimport numpy as np # specifying the plot sizeplt.figure(figsize = (10, 5)) # only one line may be specified; full heightplt.axvline(x = 7, color = 'b', label = 'axvline - full height') # rendering plotplt.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
# importing necessary librariesimport matplotlib.pyplot as pltimport numpy as np # defining an arrayxs = [1, 100] # defining plot sizeplt.figure(figsize = (10, 7)) # single lineplt.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
# importing libraryimport matplotlib.pyplot as plt # defining plot sizeplt.figure(figsize = (10, 5)) # specifying plot coordinatesplt.plot((0, 0), (0, 1), scaley = False) # setting scaley = True will make the line fit# withn the frame, i.e It will appear as a finite lineplt.show()
  Output:
 
The below methods can be used for plotting multiple lines in Python.
Method #1: Using axvline() 
Python3
# importing the modulesimport matplotlib.pyplot as pltimport numpy as np # specifying the plot sizeplt.figure(figsize = (10, 5)) # only one line may be specified; full heightplt.axvline(x = 7, color = 'b', label = 'axvline - full height') # only one line may be specified; ymin & ymax specified as# a percentage of y-rangeplt.axvline(x = 7.25, ymin = 0.1, ymax = 0.90, color = 'r',            label = 'axvline - % of full height') # place legend outsideplt.legend(bbox_to_anchor = (1.0, 1), loc = 'upper left') # rendering plotplt.show()
Output:
Method #2: Using vlines()
Python3
# importing necessary librariesimport matplotlib.pyplot as pltimport numpy as np # defining an arrayxs = [1, 100] # defining plot sizeplt.figure(figsize = (10, 7)) # multiple lines all full heightplt.vlines(x = [37, 37.25, 37.5], ymin = 0, ymax = max(xs),           colors = 'purple',           label = 'vline_multiple - full height') # multiple lines with varying ymin and ymaxplt.vlines(x = [38, 38.25, 38.5], ymin = [0, 25, 75], ymax = max(xs),           colors = 'teal',           label = 'vline_multiple - partial height') # single vline with full ymin and ymaxplt.vlines(x = 39, ymin = 0, ymax = max(xs), colors = 'green',           label = 'vline_single - full height') # single vline with specific ymin and ymaxplt.vlines(x = 39.25, ymin = 25, ymax = max(xs), colors = 'green',           label = 'vline_single - partial height') # place legend outsideplt.legend(bbox_to_anchor = (1.0, 1), loc = 'up')plt.show()
Output:
akshaysingh98088
saurabh1990aror
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python