How to get count of bar plot with non-count axis?

Question:

Below is my datasheet and sample graph

sample

As you can above x-axis is consist of day and y-axis is consist of tip and hue is set to sex.
I want the count of bar i.e for (Male)light pink number should be 8 because there are 8 male who gave tip on sunday and likewise Female should be 1. I know how to display number on the top of the graph. I don’t know how to get the count i.e 8 and 1

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="whitegrid")

df = sns.load_dataset("tips")[1:10] 
print(df)

ax = sns.barplot(x='day', y='tip',hue="sex", data=df, palette="tab20_r")

for rect in ax.patches:
    y_value = rect.get_height()
    x_value = rect.get_x() + rect.get_width() / 2
    space = 1
    label = "{:.0f}".format(y_value)
    ax.annotate(label, (x_value, y_value), xytext=(0, space), textcoords="offset points", ha='center', va='bottom')
plt.show()

Above is the code for the sample image.
If you understood my question. Any help would be great. If you didn’t undertood my question you can ask me in comment. what you didn’t understood

There are 3 questions on SO which are completely different for somewhat reason SO is marking this question as duplicate and link for the same

How to display custom values on a bar plot
How to plot and annotate grouped bars in seaborn / matplotlib
How to annotate a barplot with values from another column

Asked By: Mohit Narwani

||

Answers:

The following seems to work:

labels = df.groupby(['sex', 'day']).size()
ax = sns.barplot(x='day', y='tip',hue='sex', data=df, palette='tab20_r')
for p, value in zip(ax.patches, labels):
    x = p.get_x() + p.get_width() / 2
    y = p.get_y() + p.get_height() / 2
    ax.annotate(value, (x, y), ha='center')

Note: the order of the groupby matters; you want the "inner" group ('sex') first and the outer group ('days') last.

For the full df (not just the first 10 rows), the same code produces:

The labels contain:

>>> labels
sex     day 
Male    Thur    30
        Fri     10
        Sat     59
        Sun     58
Female  Thur    32
        Fri      9
        Sat     28
        Sun     18
dtype: int64

Alternatively:

ax = sns.barplot(x='day', y='tip',hue='sex', data=df, palette='tab20_r')
ax.bar_label(ax.containers[0], labels=labels['Male'], label_type='center')
ax.bar_label(ax.containers[1], labels=labels['Female'], label_type='center')
Answered By: Pierre D
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.