我試圖制作一個條形圖。
購買價值
| 買_咖啡 | 購買_冷飲 | 買_果汁 | 購買_糕點 | 買_三明治 | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 0 | 0 | 0 | 1 |
| 3 | 1 | 0 | 0 | 0 | 0 |
| 4 | 1 | 0 | 0 | 0 | 1 |
| 5 | 1 | 0 | 0 | 0 | 0 |
plt.bar(purchase_value.index,
purchase_value.value_counts(),
width=0.5,
bottom=None,
align='center',
color=['lightsteelblue',
'cornflowerblue',
'royalblue',
'midnightblue',
'darkblue'])
plt.xticks(rotation='vertical')
plt.show()
但事實證明
ValueError:形狀不匹配:無法將物件廣播到單個形狀
uj5u.com熱心網友回復:
當您呼叫 時
如果要為每列繪制每個專案的總和,則應使用:
fig, ax = plt.subplots()
purchase_value.sum(axis = 0).plot(kind = 'bar',
ax = ax,
stacked = True,
width=0.5,
bottom=0,
align='center',
color=['lightsteelblue',
'cornflowerblue',
'royalblue',
'midnightblue',
'darkblue'])
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409492.html
標籤:
