如何根據以下起始資料框繪制人口金字塔?
Age Gender Count
0 50-45 years male 4
1 50-45 years female 5
2 55-65 years male 6
3 55-65 years female 7
4 65-70 years male 11
5 65-70 years female 12
我嘗試了以下內容,
uj5u.com熱心網友回復:
- 與鏈接問題不同,
'Count'因為兩個'Gender'組都是正數,所以對于dodge=False,'Female'條形圖繪制在'Male'條形圖的頂部。 - 使用
.loc和布爾選擇將組之一轉換為負值。
# convert male counts to negative
df.loc[df.Gender.eq('male'), 'Count'] = df.Count.mul(-1)
# plot
sns.barplot(data=df, x='Count', y='Age', hue='Gender', orient='horizontal', dodge=False)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/350417.html
標籤:Python 熊猫 matplotlib 海生 条形图
下一篇:Linux系統編程之行程概念
