Pandas groupby年对象绘制它年复一年

时间:2014-02-15 14:29:55

标签: python matplotlib pandas

我想在12月 - 1月的12个月轴上绘制6年的12个月期间数据

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

df = pd.Series(np.random.randn(72), index=pd.date_range('1/1/2000', periods=72, freq='M'))
grouped = df.groupby(df.index.map(lambda x: x.year))

grouped.plot()

enter image description here

所以我在每年之间的界限中得到了突破。但是,我想要做的就是把年份叠加在一起。有任何简单而干净的方法吗?

1 个答案:

答案 0 :(得分:4)

可能有一个比这更好的方法:

In [44]: vals = df.groupby(lambda x: (x.year, x.month)).sum()

In [45]: vals
Out[45]: 
(2000, 1)    -0.235044
(2000, 2)    -1.196815
(2000, 3)    -0.370850
(2000, 4)     0.719915
(2000, 5)    -1.228286
(2000, 6)    -0.192108
(2000, 7)    -0.337032
(2000, 8)    -0.174219
(2000, 9)     0.605742
(2000, 10)    1.061558
(2000, 11)   -0.683674
(2000, 12)   -0.813779
(2001, 1)     2.103178
(2001, 2)    -1.099845
(2001, 3)     0.366811
...
(2004, 10)   -0.905740
(2004, 11)   -0.143628
(2004, 12)    2.166758
(2005, 1)     0.944993
(2005, 2)    -0.741785
(2005, 3)     1.531754
(2005, 4)    -1.106024
(2005, 5)    -1.925078
(2005, 6)     0.400930
(2005, 7)     0.321962
(2005, 8)    -0.851656
(2005, 9)     0.371305
(2005, 10)   -0.868836
(2005, 11)   -0.932977
(2005, 12)   -0.530207
Length: 72, dtype: float64

现在将vals上的索引更改为MultiIndex

In [46]: vals.index = pd.MultiIndex.from_tuples(vals.index)

In [47]: vals.head()
Out[47]: 
2000  1   -0.235044
      2   -1.196815
      3   -0.370850
      4    0.719915
      5   -1.228286
dtype: float64

然后取消堆积并绘图:

In [48]: vals.unstack(0).plot()
Out[48]: <matplotlib.axes.AxesSubplot at 0x1171a2dd0>

enter image description here