Search⌘ K
AI Features

Visualization with Violin Plots

Explore how to visualize categorical data using violin plots in Seaborn. Learn to display data distributions via kernel density estimation, compare categories, and customize plots with hues, ordering, scaling, and styling options to reveal important data insights.

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.

Python
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
sns.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:

Python
sns.violinplot(data = mpg_df , x ='weight')
plt.savefig('output/graph.png')

The different parts of a violin plot ...