pandas 绘制Timedelta系列的图,在选定时间带有垂直线 [英] Pandas graphing a Timedelta series, with vertical lines at selected time
问题描述
我有一个与此非常相似的问题:熊猫绘制时间序列图,在选定日期有垂直线,但是该解决方案不适用于Timedelta.
I got a very similar question to this one : Pandas graphing a timeseries, with vertical lines at selected dates but the solution doesn't works with 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
我可以这样选择本系列中的元素:
I can select element in this series like this:
In:
avg_hr[pd.Timedelta(seconds=3)]
Out:
92.9
我可以生成如下图:
In:
avg_hr.plot()
但是,我不能像这样用TimeDelta绘制垂直线:
But, I can't plot vertical lines with TimeDelta like this:
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.
Though, if I use a float or int, the vertical lines appear at position 0.
In:
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)
如何使用此timedelta索引绘制垂直线?
How can I plot vertical lines using this timedelta index?
即使我直接使用x轴上使用的键,也会遇到相同的错误:
Even if I use directly the keys used on x-axis, I got the same error:
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'
推荐答案
我发现即使我以秒为单位工作,并且轴标签以秒为单位显示时间,实际上也以纳秒为单位!
I figured out that even if I work in seconds, and that the axis label show the time in second, it's in fact in nanoseconds!
从文档熊猫时间增量:
Pandas使用64位表示纳秒级的Timedeltas 整数
Pandas represents Timedeltas in nanosecond resolution using 64 bit integers
因此,在我的问题示例中,当我调用此命令时,垂直线不在位置0处,而是实际上在110纳秒的位置(在此标度下非常接近0):
So, in the example of my question, when I called this, the vertical line was not at position 0, but in fact at position 110 nanoseconds (so very close to 0 with this scale):
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)
解决方案只是将您的x值转换为十亿分之一秒:
The solution is simply to convert your x value in nanoseconds:
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.
I found this when I tried to change the xlim, then I saw that everything was scale to nanoseconds. So, the same conversion needed to be applied to 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)
这篇关于 pandas 绘制Timedelta系列的图,在选定时间带有垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!