Home >Backend Development >Python Tutorial >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)
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)
Customization Options
You can customize the lines using these keywords:
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!