简体   繁体   中英

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). 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.

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 uses matplotlib, so you can use their functions here. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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