I have a DataFrame, group and sum that by hour, which turns it into a Series. When I plot, that, the x-axis is completely garbled, unreadable.
Summarized in code:
bicycles = both_directions.query('type == "BICYCLE"')
display(bicycles.info())
timegroups = bicycles.groupby(pd.Grouper(key='date_time', axis=0, freq="1H", sort=True)).count()['date']
display(timegroups)
display(type(timegroups.index))
timegroups.plot(kind="bar", stacked=True)
Which outputs:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2025 entries, 0 to 3588
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date_time 2025 non-null datetime64[ns]
1 speed 2025 non-null int64
2 time 2025 non-null object
3 date 2025 non-null object
4 direction 2025 non-null int64
5 length 2025 non-null float64
6 length_norm 2025 non-null int64
7 speed_norm 2025 non-null int64
8 type 2025 non-null string
dtypes: datetime64[ns](1), float64(1), int64(4), object(2), string(1)
memory usage: 158.2+ KB
None
date_time
2022-06-01 14:00:00 1
2022-06-01 15:00:00 11
2022-06-01 16:00:00 3
2022-06-01 17:00:00 8
2022-06-01 18:00:00 2
..
2022-06-13 09:00:00 0
2022-06-13 10:00:00 5
2022-06-13 11:00:00 13
2022-06-13 12:00:00 12
2022-06-13 13:00:00 13
Freq: H, Name: date, Length: 288, dtype: int64
pandas.core.indexes.datetimes.DatetimeIndex
<matplotlib.axes._subplots.AxesSubplot at 0x7fcd133c3a90>
What is the way to (smartly) skip values so that X-axis labels remain readable?
According to Panda's documentation it should already do this automatically, using default behaviour.
Pandas includes automatically tick resolution adjustment for regular frequency time-series data.
But it is clear, it doesn't in this case. What am I doing wrong? Is there a setting or conversion I'm missing? Is it a type issue (series vs dataframe?)


