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

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    dataviz decision tree poster