2016-04-28 41 views
0

我試圖從一個數據幀繪製行 -Python 3.x都有Matplotlib情節

print(df) 

Agent ID Tenure in Month As of 2014   City  State  P201101 \ 
0 1000000       65 Jersey City New Jersey 881.940392 

     P201102  P201103  P201104 P201105 P201106 ...  \ 
0 166.368261 1261.927008 531.211925 750.33716 357.73649 ...  

     P201306  P201307 P201308  P201309  P201310  P201311 \ 
0 1041.047706 639.671717 392.27343 860.148349 427.732344 972.562812 

     P201312 
0 593.675603  

我從數據幀切片的記錄代理編號1000000及現在我要繪製的月銷售數據( P201101至P201312)使用matplotlib。

下面是我的代碼 -

x = list(df.columns[4:40]) 
y= df.iloc[:,4:40].values.tolist() 
plt.plot(y[0]) 
plt.show() 

不幸的是,X軸顯示值0,5,10 ......。我想在x軸上顯示P201101,P201102,.... ,P201312。我嘗試過使用xlabel,xticks也是,但是這並不能解決問題。請幫助

enter image description here

回答

0

的問題是,我應該單獨指定x軸。正確的代碼看起來像這樣 -

x=list(range(1,37)) 
xticks = list(df.columns[4:40]) 
y= df.iloc[:,4:40].values.tolist() 
print(x,y[0]) 
plt.plot(y[0]) 
plt.title("Agent ID "+str(list(df['Agent ID']))) 
plt.xticks(x,xticks,rotation = 90) 
plt.show()