Home >Backend Development >Python Tutorial >How to Draw Vertical Lines on a Time Series Plot in Python?

How to Draw Vertical Lines on a Time Series Plot in Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-23 19:00:15559browse

How to Draw Vertical Lines on a Time Series Plot in Python?

Drawing Vertical Lines on a Plot

To overlay vertical lines on a time series plot, indicating specific time indices, there are a few approaches available.

Using plt.axvline

The simplest method is to use plt.axvline, which draws a vertical line at the specified x-coordinate. Simply provide the coordinate:

import matplotlib.pyplot as plt

plt.axvline(x=0.22058956)
plt.axvline(x=0.33088437)
plt.axvline(x=2.20589566)

Using a Loop with plt.axvline

Alternatively, you can iterate through a list of coordinates to draw multiple vertical lines:

xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
    plt.axvline(x=xc)

Both methods allow for customization of line properties such as color, style, and width using the corresponding keyword arguments.

The above is the detailed content of How to Draw Vertical Lines on a Time Series Plot in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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