Ele
Ele

Reputation: 553

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 to read.

here is a little example to understand the issue:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


df2 = pd.DataFrame({ 'A' : pd.Series(np.random.randn(4),index=list(range(4)),dtype='float32'),
                    'B' : pd.date_range('1/1/2000', periods=4)})




print(df2.to_string())
df2.ix[3,'B'] = pd.to_datetime('2005-01-02')

print(df2.to_string())

df2.index = df2.B
fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(df2.index, df2["A"])
plt.show()

the graph from 1/1/2000 to 1/3/2000 is almost unreadable, because the plot is scaled to show also the data from 2005. is there a way to eliminate that the indices (?) from 1/3/2000 to 1/3/2005?

Thanks and cheers, E.

Upvotes: 1

Views: 1116

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

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

Upvotes: 4

Related Questions