In this tutorial, we will use an example to show you how to draw vertical lines on a plot using ax.vlines() in matplotlib.
1.Import library
import matplotlib.pyplot as plt import numpy as np
2.Create a plot
fig, ax = plt.subplots(figsize=(12, 6))
3.Prepare data
np.random.seed(42) x = np.random.rand(150)
4.Draw vertical lines using ax.vlines()
ax.plot(x) ax.vlines([20, 100], 0, 1, linestyles='dashed', colors='red') plt.show()
Run this code, you will see: