#38 Show number of observation on boxplot

 

 

 

 

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

Leave a Reply

Your email address will not be published.