#58 Show number of observation on violinplot

 

 

 

 

 

It is a good practice to specify the number of observation for each group of your violinplot. Indeed, a group with 10 observation can look the same as a group with 10000, what can highly influence the message underneath.

 

 

 

 

 

 



# library & dataset
import seaborn as sns, numpy as np
df = sns.load_dataset("iris")

# Basic violinplot
ax = sns.violinplot(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.