Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas MinuteLocator shows few xticks

I have a df time of time series, for 3 trips. spread over a period of more than 1 hour. I want to visualise this on seaborn lineplot.


df.head(3)   # shows trip 533921 starting at about 18h10
    trip_id session_id  timestamp                      speed    travelmode
0   533921   865      2016-04-01 18:00:10.198000+01:00  19.0       car
1   533921   865      2016-04-01 18:00:10.198000+01:00  19.0       car
2   533921   865      2016-04-01 18:00:11.258000+01:00  19.0       car

df.tail(3) # show trip 533923 ends at about 18h52
        trip_id session_id  timestamp                       speed   travelmode
5355    533923     867     2016-04-01 18:52:11.089000+01:00 1.10223    car
5356    533923     867     2016-04-01 18:52:12.106000+01:00 1.10223    car
5357    533923     867     2016-04-01 18:52:12.106000+01:00 1.10223    car

My goal is to show the timeline of all starting and ending of trips, so:

fig, ax=plt.subplots(figsize=(20,8))
sns.lineplot(data=car_df, x='timestamp', y='speed', hue='trip_id', ci=None,  palette="bright")

xformatter = mdates.DateFormatter('%H:%M')
xlocator = mdates.MinuteLocator(byminute=[0,15], interval = 1)

## Set xtick labels
ax.xaxis.set_major_locator(xlocator)
plt.title(f'Car trips from between 18h00- 19h00 ({car_df.trip_id.nunique()} trips)')
## Format xtick labels as HH:MM
ax.xaxis.set_major_formatter(xformatter)
fig.autofmt_xdate()

Giving:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The MinuteLocator started showing the minutes intervals 17:00 17:15 but then stopped. How to I make it show until the end of the figure?

>Solution :

If you want ticks for every multiple of 15 minutes, then you need to specify that:

xlocator = mdates.MinuteLocator(byminute=[0, 15, 30, 45], interval = 1)

Presumably your data doesn’t span more than an hour, so given byminute=[0,15], only the zeroth and fifteenth minutes received a tick.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading