Basic violinplots and input formats


This post aims at describing how to realize basic violinplots. It explains how your input must be formated and which seaborn functions are to be used. Three input formats exist to draw a violinplot: one numerical variable only, one variable and several groups, and several variables.

One numerical variable only

If you have only one numerical variable, you probably better have to make an histogram or a density plot. But you can still use the violinplot function to describe the distribution of this variable, as follows:

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')
 
# Make boxplot for one group only
sns.violinplot(y=df["sepal_length"])
plt.show()

One variable and several groups

Usually, violinplots are used in cases similar to boxplots: when you have one numerical variable and several groups. It allows to compare distributions from one group to another. One usually works with two columns, one giving the value of the variable, the other one the group:

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')
 
# plot
sns.violinplot( x=df["species"], y=df["sepal_length"] )
plt.show()

Several variables

Violinplots are also useful to compare several variables. In the iris dataset, we can compare the first 2 numerical variables:

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
df = sns.load_dataset('iris')
 
# plot
sns.violinplot(data=df.iloc[:,0:2])
plt.show()

Violin

Density

Histogram

Boxplot

Ridgeline

Contact & Edit

👋 This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message onTwitter, or send an email pasting yan.holtz.data with gmail.com.

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

Violin

Density

Histogram

Boxplot

Ridgeline

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Treemap

Venn Diagram

Donut

Pie Chart

Dendrogram

Circular Packing

Line chart

Area chart

Stacked Area

Streamgraph

Timeseries with python

Timeseries

Map

Choropleth

Hexbin

Cartogram

Connection

Bubble

Chord Diagram

Network

Sankey

Arc Diagram

Edge Bundling

Colors

Interactivity

Animation with python

Animation

Cheat sheets

Caveats

3D