有时间戳的DataFrame
数据,我想直观地比较数据的每日时间演变。如果我groupby
白天并绘制图表;由于日期的不同,它们显然在时间上水平位移。
我想在仅时间轴上绘制与日期无关的日期趋势图。为此,我已将shift
数据恢复了适当的天数,如以下代码所示
import pandas as pd
import datetime
import matplotlib.pyplot as plt
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])
df = pd.DataFrame(list(range(1, len(index)+1)), index=index, columns=['a'])
gbyday = df.groupby(df.index.day)
first_day = gbyday.keys.min() # convert all data to this day
plt.figure()
ax = plt.gca()
for n,g in gbyday:
g.shift(-(n-first_day+1), 'D').plot(ax=ax, style='o-', label=str(n))
plt.show()
导致以下情节
问题:这是熊猫的做法吗?换句话说,我怎样才能更优雅地实现这一点?