2017-10-18 104 views
0

我嘗試繪製pivot_table的柱狀圖用下面的代碼:Barplot返回不受支持的操作數類型(S) - :「STR」和「浮動」

fig=plt.figure(figsize=(10,10)) 
ax1=fig.add_subplot(111) 

ax1.bar(Products.index,Products["Amount"].values) 

樞軸表是這樣的:

         Amount 
Material        
454T006S R 167 134:11    0.000000 
3457T006S R IE216 167 246:1   4.500000 
67T009S R 1510K304:1    36.000000 
7676009S R IE216 1510K304:1  30.500000 
676T012S 167 246:1     1.000000 
2324T012S R 1510 111    6.000000 
1516T012S R 1510 151    50.500000 
1516T012S R 1510 20:1    26.500000 

但它返回:

不受支持的操作數類型(一個或多個),用於: - 'STR' 和 '浮動'

我在做什麼錯了?

回答

1

問題是您的索引值是strings

我覺得simpliest是使用pandas.DataFrame.plot.bar

Products["Amount"].plot.bar() 

可以字符串的值映射到range,情節範圍和最後添加xticks

fig=plt.figure(figsize=(10,10)) 
ax1=fig.add_subplot(111) 

idx=Products.index.tolist() 
x = range(len(idx)) 

plt.bar(x, Products["Amount"].values) 
plt.xticks(x, idx, rotation=90) 
plt.show() 

類似的解決方案是reset index和情節默認index

fig=plt.figure(figsize=(10,10)) 
ax1=fig.add_subplot(111) 

Products = Products.reset_index() 
plt.bar(Products.index, Products["Amount"].values) 
plt.xticks(Products.index, Products['Material'], rotation=90) 
plt.show() 
相關問題