0

To draw plot, I am using seaborn and below is my code

import seaborn as sns

sns.set_theme(style="whitegrid")

tips = sns.load_dataset("tips")
tips=tips.head()
ax = sns.barplot(x="day", y="total_bill",hue="sex", data=tips, palette="tab20_r")

I want to get and print frequency of data plots that is no. of times it occurred and below is the expected image

To Add label in bar, I have used below code

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()

enter image description here

So, With above code. I am able to display height with respect to x-axis , but I don't want height. I want frequency/count that satisfies relationship. For above example, there are 2 male and 3 female who gave tip on Sunday. So it should display 2 and 3 and not the amount of tip

Below is the code

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="whitegrid")

df = sns.load_dataset("tips")
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()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

1
import pandas as pd
import seaborn as sns

# load the data
tips = sns.load_dataset('tips')

# determine the number of each gender for each day
tp = pd.crosstab(tips.day, tips.sex)

# or use groupby
# tp = tips.groupby(['day', 'sex']).sex.count().unstack('sex')

# plot the data
ax = sns.barplot(x='day', y='total_bill', hue='sex', data=tips)

# move the legend if needed
sns.move_legend(ax, bbox_to_anchor=(1, 1.02), loc='upper left', frameon=False)

# iterate through each group of bars, zipped to the corresponding column name
for c, col in zip(ax.containers, tp):
    
    # add bar labels with custom annotation values
    ax.bar_label(c, labels=tp[col], padding=3, label_type='center')

enter image description here

DataFrame Views

tips

tips.head()

   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

tips.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   total_bill  244 non-null    float64 
 1   tip         244 non-null    float64 
 2   sex         244 non-null    category
 3   smoker      244 non-null    category
 4   day         244 non-null    category
 5   time        244 non-null    category
 6   size        244 non-null    int64   
dtypes: category(4), float64(2), int64(1)
memory usage: 7.4 KB

tp

sex   Male  Female
day               
Thur    30      32
Fri     10       9
Sat     59      28
Sun     58      18
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158