Boxplot is an amazing way to study distributions. However, it can be useful to display the number of observation for each group since this info is hidden under boxes.
# library & dataset import seaborn as sns, numpy as np df = sns.load_dataset("iris") ax = sns.boxplot(x="species", y="sepal_length", data=df) # Calculate number of obs per group & median to position labels medians = df.groupby(['species'])['sepal_length'].median().values nobs = df['species'].value_counts().values nobs = [str(x) for x in nobs.tolist()] nobs = ["n: " + i for i in nobs] # Add it to the plot pos = range(len(nobs)) for tick,label in zip(pos,ax.get_xticklabels()): ax.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-small', color='w', weight='semibold') sns.plt.show()
Pingback: Get tick label for seaborn boxplot with hue to add sample number to a boxplot | Blog nejen pro chlapy od chlapa
Pingback: Get sample size for boxplots in seaborn factorplot | Blog nejen pro chlapy od chlapa
You should use the function df.groupby([‘species’]).size().values instead of df[‘species’].value_counts().values function.
Thanks. Works well!
Pingback: How can I calculate and place number of observations per seaborn boxplot split by hue? – Ask python questions