pandas MinuteLocator shows few xticks

Question:

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

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?

Asked By: arilwan

||

Answers:

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.

Answered By: BigBen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.