Plot vertical lines in Matplotlib

Python

Matplotlib

Line

Plot vertical

Luc B.

Python

Matplotlib

Line Plot

Vertical lines are useful for highlighting specific moments in a plot. While vertical lines can easilly be plotted with plt.plot(), Matplotlib provides a convenience function, plt.vlines(), to make plotting vertical lines easier and more readable.

Code Example

Use the plt.vlines() function to create vertical lines in Matplotlib. plt.vlines() takes three parameters, an array of x-values specifying where to plot the lines followed by the y-values the line should span. If you only want to plot one vertical line, pass a number as the first parameter instead of an array.

import matplotlib.pyplot as plt

# Create vertical lines here
plt.vlines([2, 3, 4, 5], 0, 1)

plt.show()

png