How to draw vertical lines on a given plot in matplotlib?
Given a plot of signal in time representation, how to draw lines marking corresponding time index?
Specifically, given a signal plot with time index ranging from 0 to 2.6(s), I want to draw vertical red lines indicating corresponding time index for the list
[0.22058956, 0.33088437, 2.20589566], how can I do it?
The standard way to add vertical lines that will cover your entire plot window without you having to specify their actual height is
import matplotlib.pyplot as plt plt.axvline(x=0.22058956) plt.axvline(x=0.33088437) plt.axvline(x=2.20589566)
xcoords = [0.22058956, 0.33088437, 2.20589566] for xc in xcoords: plt.axvline(x=xc)
You can use many of the keywords available for other plot commands (e.g.
linewidth ...). You can pass in keyword arguments
ymax if you like in axes corrdinates (e.g.
ymax=0.75 will cover the middle half of the plot). There are corresponding functions for horizontal lines (
axhline) and rectangles (
★ Back to homepage or read more recommendations: