简体   繁体   English

在熊猫图上添加y轴线

[英]Add y-axis line on pandas plot

I have results of sport event(run) as lap times. 我将体育赛事的结果作为跑步时间。 The time is in format of H:M:S(00:49:51). 时间格式为H:M:S(00:49:51)。 I have plotted the most common finishing time like this: 我已经画出了最常见的整理时间,如下所示:

df['new_time'] = pd.to_timedelta(df['time'], errors='coerce')
time_frame = str(60 * 5) + 's'
lap_time = df.groupby(pd.Grouper(key='new_time', freq=time_frame)).count()
lap_time['count'].plot()

and it works fine. 而且效果很好。 在此处输入图片说明

But I want to draw red line or mark, where would my result time(01:07:45) be.Other option would also be to change the plot to bars and just color that one bar red where my time would be groupbed in. 但是我想画一条红色的线或标记,我的结果时间(01:07:45)会在哪里,另一种选择是将绘图更改为小节,只将一个小节涂成红色,以便将时间分组。

EDIT: This is double question . 编辑:这是双重问题 The probelm was that Pandas represents Timedeltas in nanosecond resolution using 64 bit integers and thats why it didn't show up. 问题是,Pandas使用64位整数以纳秒分辨率表示Timedeltas,这就是为什么它没有出现的原因。

Pandas uses matplotlib, so you can use their functions here. 熊猫使用matplotlib,因此您可以在此处使用其功能。 Add the following lines to your code: 将以下行添加到您的代码中:

df['new_time'] = pd.to_timedelta(df['time'], errors='coerce')
time_frame = str(60 * 5) + 's'
lap_time = df.groupby(pd.Grouper(key='new_time', freq=time_frame)).count()
ax = lap_time['count'].plot()

ymin, ymax = ax.get_ylim()
date # make date be equal to whatever you are storing in your DataFrame
# If it is of type; datetime.time, then the following should work
# date = pd.to_datetime('01:07:45').time()
ax.vlines(x=date, ymin=ymin, ymax=ymax-1, color='r')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM