Matplotlib: Draw Vertical Lines on Plot
Introduction
Matplotlib is one of the most widely used data visualization libraries in Python. Much of Matplotlib’s popularity comes from its customization options – you can tweak just about any element from its hierarchy of objects.
In this tutorial, we’ll take a look at how to draw a vertical line on a Matplotlib plot, that allows us to mark and highlight certain regions of the plot, without zooming or changing the axis range.
Creating a Plot
Let’s first create a simple plot with some random data:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(12, 6))
np.random.seed(42)
x = np.random.rand(150)
ax.plot(x)
plt.show()
Here, we’ve used Numpy to generate 150 random data points, in a range of [0, 1)
.