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
# importing the modules import matplotlib.pyplot as plt import numpy as np # specifying the plot size plt.figure(figsize = ( 10 , 5 )) # only one line may be specified; full height plt.axvline(x = 7 , color = 'b' , label = 'axvline - full height' ) # rendering plot 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
# importing necessary libraries import matplotlib.pyplot as plt import numpy as np # defining an array xs = [ 1 , 100 ] # defining plot size plt.figure(figsize = ( 10 , 7 )) # single line 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
# importing library import matplotlib.pyplot as plt # defining plot size plt.figure(figsize = ( 10 , 5 )) # specifying plot coordinates plt.plot(( 0 , 0 ), ( 0 , 1 ), scaley = False ) # setting scaley = True will make the line fit # within the frame, i.e It will appear as a finite line 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
# importing the modules import matplotlib.pyplot as plt import numpy as np # specifying the plot size plt.figure(figsize = ( 10 , 5 )) # only one line may be specified; full height plt.axvline(x = 7 , color = 'b' , label = 'axvline - full height' ) # only one line may be specified; ymin & ymax specified as # a percentage of y-range plt.axvline(x = 7.25 , ymin = 0.1 , ymax = 0.90 , color = 'r' , label = 'axvline - % of full height' ) # place legend outside plt.legend(bbox_to_anchor = ( 1.0 , 1 ), loc = 'upper left' ) # rendering plot plt.show() |
Output:
Method #2: Using vlines()
Python3
# importing necessary libraries import matplotlib.pyplot as plt import numpy as np # defining an array xs = [ 1 , 100 ] # defining plot size plt.figure(figsize = ( 10 , 7 )) # multiple lines all full height plt.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 ymax plt.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 ymax plt.vlines(x = 39 , ymin = 0 , ymax = max (xs), colors = 'green' , label = 'vline_single - full height' ) # single vline with specific ymin and ymax plt.vlines(x = 39.25 , ymin = 25 , ymax = max (xs), colors = 'green' , label = 'vline_single - partial height' ) # place legend outside plt.legend(bbox_to_anchor = ( 1.0 , 1 ), loc = 'up' ) plt.show() |
Output: