如何在matplotlib中的给定图上绘制垂直线? [英] How to draw vertical lines on a given plot in matplotlib?
问题描述
给定时间表示中的信号图,如何绘制标记相应时间索引的线?
Given a plot of signal in time representation, how to draw lines marking corresponding time index?
具体来说,给定时间索引范围从0到2.6(s)的信号图,我想绘制垂直红线以指示列表[0.22058956, 0.33088437, 2.20589566]
的相应时间索引,我该怎么办?
Specifically, given a signal plot with time index ranging from 0 to 2.6(s), I want to draw vertical red lines indicating corresponding time index for the list [0.22058956, 0.33088437, 2.20589566]
, how can I do it?
推荐答案
添加垂直线以覆盖整个绘图窗口而无需指定其实际高度的标准方法是plt.axvline
The standard way to add vertical lines that will cover your entire plot window without you having to specify their actual height is plt.axvline
import matplotlib.pyplot as plt
plt.axvline(x=0.22058956)
plt.axvline(x=0.33088437)
plt.axvline(x=2.20589566)
OR
xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
plt.axvline(x=xc)
您可以使用许多其他绘图命令可用的关键字(例如color
,linestyle
,linewidth
...).如果您喜欢在坐标轴中插入坐标,则可以传入关键字参数ymin
和ymax
(例如ymin=0.25
,ymax=0.75
将覆盖图形的中间部分).水平线(axhline
)和矩形(axvspan
)有相应的功能.
You can use many of the keywords available for other plot commands (e.g. color
, linestyle
, linewidth
...). You can pass in keyword arguments ymin
and ymax
if you like in axes corrdinates (e.g. ymin=0.25
, ymax=0.75
will cover the middle half of the plot). There are corresponding functions for horizontal lines (axhline
) and rectangles (axvspan
).
这篇关于如何在matplotlib中的给定图上绘制垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!