【发布时间】:2017-07-21 16:44:57
【问题描述】:
我有一个与这个问题非常相似的问题:Pandas graphing a timeseries, with vertical lines at selected dates,但该解决方案不适用于 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()
但是,我不能像这样用 TimeDelta 绘制垂直线:
In:
plt.axvline(x=pd.Timedelta(seconds=110), color='r', linestyle='dashed', linewidth=2)
Out:
TypeError: Cannot compare type 'Timedelta' with type 'float64'
不过,如果我使用浮点数或整数,垂直线会出现在位置 0。
In:
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)
如何使用这个 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'
【问题讨论】:
-
avg_hr.index.dtype返回什么? -
@Vinícius Aguiar:输出:timedelta64[ns]
标签: python pandas matplotlib timedelta