jB777
jB777

Reputation: 85

Pandas dataframe plot time axis format total seconds into elapsed HH:MM:SS

I have a pandas dataframe where index value are integers 0-10000 and each value corresponds to 1 second. I have 1 temp column in dataframe. I can create a plot of temp vs time using matplotlib:

import matplotlib.pyplot as plt
df.plot()
plt.show()

With this time is displayed on x-axis in seconds (index value), I'd like to display time in a more readable way for example 50min 5 sec, instead of 3005 seconds. Thanks for any help

Upvotes: 1

Views: 1038

Answers (1)

tdy
tdy

Reputation: 41327

index value are integers 0-10000 and each value corresponds to 1 second

Convert the index to_timedelta and specify the unit as seconds:

df = pd.DataFrame({'temp': np.random.randn(10_001).cumsum()})

df.index = pd.to_timedelta(df.index, unit='seconds')
df.plot(figsize=(10, 3))

figure with timedelta axis

Upvotes: 1

Related Questions