Home > Article > Backend Development > How to Draw Vertical Reference Lines on Time Series Plots?
Drawing Vertical Reference Lines on Time Series Plots
When analyzing time-series data, it often becomes necessary to create reference lines at specific time points to highlight or mark events. This article demonstrates how to draw such vertical reference lines on a given time-series plot, providing a step-by-step guide with code examples.
Problem:
Given a time-series plot with a time index ranging from 0 to 2.6 seconds, we want to draw vertical red lines indicating the corresponding time index for a set of specified time values, such as [0.22058956, 0.33088437, 2.20589566].
Solution:
To add vertical reference lines that span the entire plot height, we can use the plt.axvline function:
import matplotlib.pyplot as plt plt.axvline(x=0.22058956) plt.axvline(x=0.33088437) plt.axvline(x=2.20589566)
Alternatively, if we have a list of time values, we can loop through them and draw a vertical line for each one:
xcoords = [0.22058956, 0.33088437, 2.20589566] for xc in xcoords: plt.axvline(x=xc)
These functions accept additional keyword arguments such as color, linestyle, and linewidth for customizing the appearance of the reference lines. Additionally, the ymin and ymax keywords can be used to specify a specific range in the plot to cover.
Additional Notes:
The above is the detailed content of How to Draw Vertical Reference Lines on Time Series Plots?. For more information, please follow other related articles on the PHP Chinese website!