LivingstoneM
LivingstoneM

Reputation: 1088

Add labels as percentages instead of counts on a grouped bar graph in seaborn

I have a categorical dataset which I am plotting a bar graph using seaborn. However I am unable to add labels on top of bars in percentage value. Here is how the dataset looks like:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Day': ['Mon', 'Tue', 'Wed', 'Thur', 'Fri',
                           'Mon', 'Tue', 'Wed', 'Thur', 'Fri'],
                   'Customers': [44, 46, 49, 59, 54,
                                 33, 46, 50, 49, 60],
                   'Time': ['M', 'M', 'M', 'M', 'M',
                            'E', 'E', 'E', 'E', 'E']})

#view DataFrame
df

This is the code for plotting grouped bar graph:

import matplotlib.pyplot as plt
import seaborn as sns

#set seaborn plotting aesthetics
sns.set(style='white')

#create grouped bar chart
sns.barplot(x='Day', y='Customers', hue='Time', data=df) 

Now I would like to add labels on top of bars but in percentage value. Kindly help

Upvotes: 1

Views: 6217

Answers (1)

tdy
tdy

Reputation: 41327

Use groupby.transform to compute the split percentages per day:

df['Customers (%)'] = df.groupby('Day')['Customers'].transform(lambda x: x / x.sum() * 100)

#     Day  Customers Time  Customers (%)
# 0   Mon         44    M      57.142857
# 1   Tue         46    M      50.000000
# 2   Wed         49    M      49.494949
# 3  Thur         59    M      54.629630
# 4   Fri         54    M      47.368421
# 5   Mon         33    E      42.857143
# 6   Tue         46    E      50.000000
# 7   Wed         50    E      50.505051
# 8  Thur         49    E      45.370370
# 9   Fri         60    E      52.631579

Then plot this new Customers (%) column and label the bars using ax.bar_label (with percentage formatting via the fmt param):

ax = sns.barplot(x='Day', y='Customers (%)', hue='Time', data=df) 

for container in ax.containers:
    ax.bar_label(container, fmt='%.0f%%')

Note that ax.bar_label requires matplotlib 3.4.0.

Upvotes: 5

Related Questions