1

I have a large pandas dataframe like this:

In:

d = {'type': ['type 1', 'type 2', 'type 2', 'type 3', 'type 1', 'type 4', 'type 5', 'type 6', 'type 6', 'type 7' ], 'value': ['yes', 'no', 'yes', 'no', 'yes', 'no', 'yes','no', 'yes', 'no']}
df = pd.DataFrame(data=d)
df

Out:

    type     value
0   type: 1   yes
1   type: 2   no
2   type: 2   yes
3   type: 3   no
4   type: 1   yes
5   type: 4   no
6   type: 5   yes
7   type: 6   no
8   type: 6   yes
9   type: 7   no

How can I create an horizontal barplot with the percentage of yes/no values in each bar? So far I tried to:

sns.barplot(x='type', y='value', data=df,  orient = 'h')

However, I only get the bars with no percentage of the distribution for each yes/no value. How can I draw it in the same bar without splitting it? For example:

example

In the above example, the total count of yes/no is in the x axis.

J Do
  • 121
  • 12

1 Answers1

1

You can use pd.crosstab to compute the percentage, then plot.barh to plot the bars:

ax = (pd.crosstab(df['type'], df['value'], normalize='index')
        .plot.barh(stacked=True, alpha=0.8)
     )

Output:

enter image description here

For the numbers on bars, have a look at this question.

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74