Skip to content Skip to sidebar Skip to footer

Pandas Plot Aggregate Timestamp Index

I have a timeseries of data I would like to plot. In the night, when i do not collect data, I have a gap between 9 pm and 7 am which looks a bit ugly on the chart and makes it hard

Solution 1:

IIUC, let me create a sample set and bad outcome.

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))
df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]
df2.plot()

Output:

enter image description here

However, we can eliminate those flatline sections like this:

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))

df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]

df2.index = df2.index.strftime('%Y-%m-%d')

fig, ax = plt.subplots()
_ = df2.plot(ax=ax)
skip = df2.shape[0]//7 + 1
label = [i for i in df2.index[::skip]]
_ = plt.xticks(np.arange(0,df2.shape[0],skip),label,rotation=45)

Output:

enter image description here

Post a Comment for "Pandas Plot Aggregate Timestamp Index"