2016-04-28 81 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()