Matplotlib: Draw Vertical Lines on Plot



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))

x = np.random.rand(150)

Here, we’ve used Numpy to generate 150 random data points, in a range of [0, 1).


To finish reading, please visit source site