Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn how to add number of samples per category in sns.catplot

I have a catplot drawing using:

s = sns.catplot(x="level", y="value", hue="cond", kind=graph_type, data=df)

enter image description here

However, the size of the groups is not equal: "Minimal" has n=12 samples , and "Moderate" has n=18 samples.

How can I add this info to the graph?

like image 667
okuoub Avatar asked Dec 04 '25 00:12

okuoub


1 Answers

Manually calculate the sizes and add them to xticklabels, something like this

import matplotlib.pyplot as plt
import seaborn as sns

exercise = sns.load_dataset("exercise")
cnts = dict(exercise['time'].value_counts())
key = list(cnts.keys())
vals = list(cnts.values())

g = sns.catplot(x="time", y="pulse", hue="kind",order=key,
            data=exercise, kind="box")
g.set_axis_labels("", "pulse")
g.set_xticklabels([(key[i]+'\n('+str(vals[i])+')') for i in range(len(key))])
plt.show()  

enter image description here

like image 61
Julkar9 Avatar answered Dec 06 '25 16:12

Julkar9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!