2016-10-12 26 views
2

我有一個數據集,看起來像這樣:pandas:如何在df.plt()中很好地格式化時間戳軸標籤?

  prod_code  month items  cost 
0 040201060AAAIAI 2016-05-01  5 572.20 
1 040201060AAAKAK 2016-05-01 164 14805.19 
2 040201060AAALAL 2016-05-01 13465 14486.07 

df.dtypes表明month列是datetime64[ns]類型。

現在我想要繪製每月的費用爲特定的產品:

df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].plot() 
plt.show() 

這工作,但X軸是不是我所期待的時間戳:

enter image description here

如何使用月份和年份標籤很好地格式化x軸標籤?

更新:我也試過了,得到柱狀圖,其確實在x軸輸出的時間戳,但在很長的笨重格式:

df[df.bnf_code=='040201060AAAIAI'].plot.bar(x='month', y='cost', title='Spending on 040201060AAAIAI') 

回答

2

如果設置的日期爲指標, x軸應標記正確:

df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].set_index('month').plot() 

我只是簡單地將​​添加到您的代碼中。