Cranjis
Cranjis

Reputation: 1960

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?

Upvotes: 3

Views: 689

Answers (1)

Julkar9
Julkar9

Reputation: 2110

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

Upvotes: 3

Related Questions