Pandas 绘制 Timedelta 系列图表,选定时间位置添加垂直线

3
我可以帮您翻译。以下是翻译结果,保留html标签:

我有一个非常类似的问题:Pandas图形化时间序列,并在选定日期处绘制垂直线,但该解决方案不能使用Timedelta。

考虑这个系列:

In:
avg_hr.head()

Out:
00:00:00     69.000000
00:00:01     93.750000
00:00:02     93.125000
00:00:03     92.900000
00:00:04     93.222222
00:00:05     93.222222
...
Name: bpm, Length: 253, dtype: float64

我可以像这样选择此系列中的元素:

In:
avg_hr[pd.Timedelta(seconds=3)]

Out:
92.9

我可以生成这样的图表:
In:
avg_hr.plot()

avg_hr.plot()

但是,我无法像这样使用TimeDelta绘制垂直线:

In:
plt.axvline(x=pd.Timedelta(seconds=110), color='r', linestyle='dashed', linewidth=2)

Out:
TypeError: Cannot compare type 'Timedelta' with type 'float64'

但是,如果我使用float或int类型,竖线会出现在位置0。

In:
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)

avg_hr.plot()

如何使用timedelta索引绘制垂直线?

编辑:

即使我直接使用x轴上使用的键,我也会得到相同的错误:

In:
for key in avg_hr.keys():
    ax.axvline(x=key, color='r', linestyle='dashed', linewidth=2)

Out:
TypeError: Cannot compare type 'Timedelta' with type 'float64'

1
avg_hr.index.dtype 返回的是什么? - Vinícius Figueiredo
1
@Vinícius Aguiar: 输出:timedelta64[ns] - probitaille
2个回答

10
我发现即使我在以秒为单位工作,坐标轴标签显示的时间也是以秒为单位,但实际上是以纳秒为单位!
根据Pandas Time Deltas文档:
Pandas使用64位整数以纳秒分辨率表示Timedeltas。
因此,在我的问题示例中,当我调用此函数时,垂直线不是在位置0处,而实际上是在位置110纳秒处(因此在此刻度下非常接近0)。
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)

解决方案就是将你的x值转换成纳秒:

x_ns = pd.Timedelta(seconds=110) / pd.Timedelta(1,'ns') #Seconds to nanoseconds
plt.axvline(x=x_ns, color='r', linestyle='dashed', linewidth=2)

当我尝试更改xlim时,发现所有东西都被缩放到了纳秒。因此,需要应用相同的转换来适用于xlim。

ax1.set_xlim([0, 110])

这里输入图片描述

多条垂直线的结果

这里输入图片描述

完成于:

#Add verticals lines for specific event
plt.axvline(x=pd.Timedelta(seconds=120) / pd.Timedelta(1,'ns'), color='r', linestyle='dashed', linewidth=2)
plt.axvline(x=pd.Timedelta(seconds=185) / pd.Timedelta(1, 'ns'), color='r', linestyle='dashed', linewidth=2)
plt.axvline(x=pd.Timedelta(seconds=210) / pd.Timedelta(1, 'ns'), color='r', linestyle='dashed', linewidth=2)
plt.axvline(x=pd.Timedelta(seconds=225) / pd.Timedelta(1, 'ns'), color='r', linestyle='dashed', linewidth=2)

1
非常整洁,你可以改变一下你的数据框格式,看起来像这样:avg_hr = pd.DataFrame({'data':pd.Series(np.random.randn(240)).cumsum(),'time': pd.to_timedelta(np.arange(240), unit='s')})avg_hr.plot()plt.axvline(x=110,color='r', linestyle='dashed', linewidth=2)plt.show()效果很好。 - Vinícius Figueiredo

0

我遇到了一个类似的问题。我使用的TimeDelta索引的解决方案是total_seconds属性,它以秒为单位返回浮点数。(“TimeDelta的总持续时间(以纳秒精度)”)

所以,

plt.axvline(pd.Timedelta(seconds=120).total_seconds, color='r', linestyle='dashed', linewidth=2)

应该可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接