52. Matplotlib: Violin plots

This post is also available as a PDF and as a Jypyter Notebok.

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()

plot_23

 

One thought on “52. Matplotlib: Violin plots

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s