Violin plots are an alternative to box plots. They show the spread of data in the form of a distribution plot along the y axis. Some people love them. Others don’t! See what you think.

import numpy as np
import matplotlib.pyplot as plt
n_violins = 5
groups = np.arange(1,n_violins+1)
# Use Python list comprehension to build distributions
# Mean is i (group #), standard deviation is 0.5 * i
samples = [np.random.normal(3*i,0.5*i,250) for i in groups]
violins = plt.violinplot (samples,
groups,
points=300, # the more the smoother
widths=0.8,
showmeans=False,
showextrema=True,
showmedians=True)
# Change the bodies to grey
for v in violins['bodies']:
v.set_facecolor('0.8')
v.set_edgecolor('k')
v.set_linewidth(1)
v.set_alpha(1)
# Make all the violin statistics marks red:
for partname in ('cbars','cmins','cmaxes','cmedians'):
vp = violins[partname]
vp.set_edgecolor('r')
vp.set_linewidth(1)
plt.show()

Interests are use of simulation and machine learning in healthcare, currently working for the NHS and the University of Exeter. Committed to all work being performed in Free and Open Source Software (FOSS), and as much source data being made available as possible.
https://gitlab.com/michaelallen1966
View all posts by Michael Allen

## One thought on “52. Matplotlib: Violin plots”