How to plot a vertical line in python

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:

How to plot a vertical line in python

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:

How to plot a vertical line in python

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:

How to plot a vertical line in python

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:

How to plot a vertical line in python

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:

How to plot a vertical line in python


How do I make a vertical line in Python?

By using axvline() In matplotlib, the axvline() method is used to add vertical lines to the plot. The above-used parameters are described as below: x: specify position on the x-axis to plot the line. ymin and ymax: specify the starting and ending range of the line.

How do you plot a vertical line in pandas?

Using panda we can create a data frame..
Creating a data frame would help to create help..
Using axvline(), add a vertical line across the axes, where color is green, linestyle="dashed"..
Using axvline(), add a vertical line across the axes, where color is red, linestyle="dashed"..
Using plt. show(), show the plot..

How do you plot vertically?

To graph a vertical line that goes through a given point, first plot that point. Then draw a straight line up and down that goes through the point, and you're done!

How do you plot a line in Python?

Simple Line Plots.
%matplotlib inline import matplotlib.pyplot as plt plt. style. use('seaborn-whitegrid') import numpy as np. ... .
fig = plt. figure() ax = plt. axes() ... .
In [3]: fig = plt. figure() ax = plt. ... .
In [4]: plt. plot(x, np. ... .
In [5]: plt. plot(x, np. ... .
plt. plot(x, x + 0, '-g') # solid green plt. ... .
In [9]: plt. ... .
In [10]: plt..