Visualization with Violin Plots
Learn how to visualize data distribution and presentation through violin plots.
We'll cover the following...
Overview
A violin plot is a categorical distribution plot similar to a box plot. However, in contrast to a box plot (which plots the actual data points), the violin plot represents data distribution using the kernel density estimation (KDE) technique. The KDE estimates the probability density function in a nonparametric manner, which means that we make no assumptions about the underlying distribution of our data.
Plotting the violin plot
To get started with our visualizations, we import the required libraries and the mpg
dataset from seaborn and view the data using the head()
method.
import seaborn as snsimport pandas as pdimport matplotlib.pyplot as pltsns.set_theme()mpg_df = sns.load_dataset('mpg')print(mpg_df.head())
Let’s plot a violin plot of a single variable weight
using the sns.violinplot()
function. The plot is shown below:
sns.violinplot(data = mpg_df , x ='weight')plt.savefig('output/graph.png')
The different parts of a violin plot are demonstrated below. The small dot we see in the plot is the ...