Home > Backend Development > Python Tutorial > How to Add Vertical Lines to Time-Series Plots in Matplotlib?

How to Add Vertical Lines to Time-Series Plots in Matplotlib?

Susan Sarandon
Release: 2024-11-16 09:22:02
Original
857 people have browsed it

How to Add Vertical Lines to Time-Series Plots in Matplotlib?

Adding Vertical Lines to Time-Series Plots

Problem:

Given a time-series plot, how can you draw vertical lines at specific time indices to mark significant events or intervals?

Solution:

Using the 'axvline' Function

Matplotlib's standard method for adding vertical lines that span the entire plot window is the '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, you can provide a list of time indices and iterate through it:

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

Customization Options

You can customize the lines using these keywords:

  • 'color': RGB values, html color names, or colored line styles (e.g. 'r' for red)
  • 'linestyle': Solid, dotted, dashed, or dash-dotted lines (e.g. '-', '--', ':')
  • 'linewidth': The line width in points
  • 'ymin' and 'ymax': Coordinates to specify the beginning and end of the vertical line (in axes coordinates)

The above is the detailed content of How to Add Vertical Lines to Time-Series Plots in Matplotlib?. 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