我有以下格式的熊貓資料框:
df = pd.DataFrame([['A', 'Blue', 30], ['A', 'Red', 180], ['B', 'Blue', 50], ['B', 'Red', 340], ['C', 'Blue', 70], ['C', 'Red', 930]])
df.columns = ['Type', 'Color', 'Count']
df:
Type Color Count
0 A Blue 30
1 A Red 180
2 B Blue 50
3 B Red 340
4 C Blue 70
5 C Red 930
期待:
我想要一個像這樣的直方圖:
1) x is 'Type' and y is 'Count'
2) 'Red' and 'Blue' in one combined bar for each type
3) 'Blue' is on top of 'Red', with different colors.
我試過了:
df.plot.bar(x='Type', y='Count', rot=0)
但通過這種方式,“紅色”和“藍色”位于不同的條中,顏色相同。

這不是我所期望的。
任何指導表示贊賞
uj5u.com熱心網友回復:
IIUC,你想要:
>>> df.pivot("Type","Color","Count")[["Red", "Blue"]].plot.bar(stacked=True, color=["red", "blue"], legend=False, width=1)
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/315270.html
