plotting data for different days on a single HH:MM:SS axis

北战南征 提交于 2019-12-05 22:45:01

You can select the hour attribute of the index after grouping like this:

In [36]: fig, ax = plt.subplots()
In [35]: for label, s in gbyday:
   ....:     ax.plot(s.index.hour, s, 'o-', label=label)

It might be a little too late for this answer, but in case anyone is still looking for it.

This solution works on different months (it was an issue if using the code from the original question) and keeps fractional hours.

import pandas as pd
import matplotlib.pyplot as plt

index0 = pd.date_range('20141101', freq='H', periods=2)
index1 = pd.date_range('20141201', freq='H', periods=2)
index2 = pd.date_range('20141210', freq='2H', periods=4)
index3 = pd.date_range('20141220', freq='3H', periods=5)

index = index1.append([index2, index3, index0])
df = pd.DataFrame(list(range(1, len(index)+1)), index=index, columns=['a'])


df['time_hours'] = (df.index - df.index.normalize()) / pd.Timedelta(hours=1)

fig, ax = plt.subplots()
for n,g in df.groupby(df.index.normalize()):
    ax.plot(g['time_hours'], g['a'], label=n, marker='o')

ax.legend(loc='best')
plt.show()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!