#55 Control order of groups in violinplot | seaborn

Here are 2 tips to order your seaborn violinplot.

1/ Give a specific order
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# plot
sns.violinplot(x='species', y='sepal_length', data=df, order=[ "versicolor", "virginica", "setosa"])
2/ Order by decreasing median
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Find the order
my_order = df.groupby(by=["species"])["sepal_length"].median().iloc[::-1].index

# Give it to the violinplot
sns.violinplot(x='species', y='sepal_length', data=df, order=my_order)

	

Leave a Reply

Your email address will not be published.