How to Create a Violin Plot with Matplotlib

12.26.2020

Intro

A violin plot allows you to view both the distribution of data as well as the quantiles, mean/median, min and max values. It combines both the historgram and box plot into one. In this article we will see how to create a violin plot with MatPlotLib.

Creating the Violin Plot

To create a violin plot, we can use the violinplot method from pyplot. We use an array of tire pressures (which is the example data used in our histrogram and boxplot article). We can see the resulting plot is the box plot with the distributions on the left and right side.

import matplotlib.pyplot as plt
import numpy as np 
  
  
tirePressure = np.array([
	22, 58, 5, 43, 56,  
	73, 55, 54, 11, 
	20, 51, 5, 79, 31, 
	27
]) 

plt.violinplot(tirePressure, showmeans=True)
plt.show()