Drawing Vertical Lines on a Plot
To visualize specific time points on a signal plot, one can draw vertical lines indicating their corresponding time indices. Here's how to achieve this in Python using Matplotlib:
Problem:
Given a plot of a signal representing time indices ranging from 0 to 2.6 seconds, how do you draw vertical red lines to mark time indices [0.22058956, 0.33088437, 2.20589566]?
Solution:
The most common method is to use the plt.axvline function:
import matplotlib.pyplot as plt plt.axvline(x=0.22058956, color='red') plt.axvline(x=0.33088437, color='red') plt.axvline(x=2.20589566, color='red')
Alternatively, you can use a loop:
xcoords = [0.22058956, 0.33088437, 2.20589566] for xc in xcoords: plt.axvline(x=xc, color='red')
These functions allow you to customize the lines by specifying various parameters like color, linestyle, and linewidth. Additionally, you can use the ymin and ymax keywords to control the height of the lines.
The above is the detailed content of How to Draw Vertical Lines on a Signal Plot to Mark Specific Time Indices in Python?. For more information, please follow other related articles on the PHP Chinese website!