2016-05-16 59 views
1

matplotlibの折れ線グラフピボットテーブル

import pandas as pd 
import numpy as np 
%matplotlib inline 
df = pd.DataFrame(
     {'YYYYMM':[201603,201503,201403,201303,201603,201503,201403,201303], 
     'Count':[5,6,2,7,4,7,8,9], 
     'Group':['A','A','A','A','B','B','B','B']}) 
df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2].astype(np.int64) 
t=df.pivot_table(df,index=['YYYYMM'],columns=['Group'],aggfunc=np.sum) 


    Count 
Group A B 
YYYYMM  
2013 7 9 
2014 2 8 
2015 6 7 
2016 5 4 

Iは長年反射y軸目盛りラベルと線グラフを作成し、それをプロットする(YYYYMM)ピボットテーブルに示されているように。ここで

は、私がこれまで持っているものです。

fig, ax = plt.subplots(1,1) 
t.plot(ax=ax) 

軸ラベルではなく、そこにあるもので、それぞれ、2013、2014、2015、および2016でなければなりません。事前に

enter image description here

ありがとう!

答えて

1

私はあなたが.astype(np.int64)を削除することができると思いますが、あなたは、インデックスstring内の値の型を取得:

df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2] 

print t.index 
Index([u'2013', u'2014', u'2015', u'2016'], dtype='object', name=u'YYYYMM') 

しかし、あなたはintインデックス、使用が必要な場合:

from matplotlib.ticker import FuncFormatter 

fig, ax = plt.subplots(1,1) 
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x))) 
t.plot(ax=ax) 
関連する問題