Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

python - plot year over year on 12 month axis

I want to plot 6 years of 12 month period data on one 12 month axis from Dec - Jan.

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'))

# display(df.head())
2000-01-31    0.713724
2000-02-29    0.416233
2000-03-31   -0.147765
2000-04-30    0.141021
2000-05-31    0.966261
Freq: M, dtype: float64

grouped = df.groupby(df.index.map(lambda x: x.year))

grouped.plot()

enter image description here

I'm getting the breaks in the lines between each year. However, what I want to do is have the year stacked over each other. Any simple and clean ways to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's probably a better way than this:

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

Now change the index on vals to a 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

Then unstack and plot:

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...