2017-03-05 36 views
2

我有一個數據框,我篩選出的列獲得x,並確實 y = vl.groupby(['Model Year','Manufacturer']).size()得到值y。 現在,當我嘗試繪製它時,我得到了一個錯誤。這是我第一次試圖錯過某些東西。你可以幫我嗎。我怎麼會用這個數據繪製一個條形圖

#ax = plt.subplot(111) 
#ax.bar(x, y,width=0.1,color='b',align='center') 

#plt.show() 

x: array(['1991', '1992', '1993', '1994'], dtype=object) 

y: 1991  Chevrolet    1 
      Ford     1 
1992  Chevrolet    2 
      Dodge     2 
      Ford     1 
1993  Chevrolet    3 
      Dodge     2 
      Ford     2 
1994  Dodge     3 
      Ford     2 
dtype: int64 

我試圖繪製的是年份與製造商的汽車數量。

回答

2

我認爲你需要添加unstack創建與MultiIndex末級列,然後DataFrame.plot.bar

print (y.unstack(fill_value=0)) 
     Chevrolet Dodge Ford 
1991   1  0  1 
1992   2  2  1 
1993   3  2  2 
1994   0  3  2 

y = vl.groupby(['Model Year','Manufacturer']).size() 
y.unstack(fill_value=0).plot.bar(width=0.1, align='center') 

plt.show() 

graph1

crosstab另一種可能的解決方案:

pd.crosstab(vl['Model Year'], vl['Manufacturer']).plot.bar(width=0.1, align='center') 
plt.show() 
相關問題