How to plot a population pyramid based on the following starting dataframe?
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
CodePudding user response:
- Unlike in the linked question,
'Count'
for both'Gender'
groups is positive, so withdodge=False
, the'Female'
bars are drawn on top of the'Male'
bars. - Convert one of the groups to negative values, using
.loc
and Boolean selection.
# 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)