How to Draw Vertical Reference Lines on Time Series Plots?

Mary-Kate Olsen
Release: 2024-11-17 22:07:01
Original
634 people have browsed it

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)
Copy after login

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)
Copy after login

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:

  • Corresponding functions for horizontal lines (axhline) and rectangles (axvspan) are also available.
  • Vertical and horizontal reference lines can be useful for comparing different time points, identifying patterns, and annotating data.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template