Custom seaborn violinplot

logo of a chart:Violin

You can custom some features of seaborn violinplots. Here are two examples showing how to change the linewidth and the general width of each group.

Change line width

Depending on visual preferences, you may want to accentuate the distribution contour. This can be performed by setting the value of the 'linewidth' parameter.


# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 300
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')
 
# Change line width
sns.violinplot(x=df["species"], y=df["sepal_length"], linewidth=5)
plt.show()

Change the general width

In the case where you have multiple distributions to display on the same graph and you want to be sure that they do not overlap, you can also change the 'width' parameter.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')

# Change width
sns.violinplot(x=df["species"], y=df["sepal_length"], width=0.3)
plt.show()

Empty violins

Thanks to the fill argument, we can easily remove the background of the violins:

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')

# Change width
sns.violinplot(x=df["species"], y=df["sepal_length"], fill=False)
plt.show()

Saturation

A simple way to improve the style of a violin is to lower the saturation level (default 1):

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
  
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')

# Change width
sns.violinplot(x=df["species"], y=df["sepal_length"], saturation=0.2)
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!