42. Matplotlib: Boxplots

Matplotlib allows easy creation of boxplots. These traditionally show median (middle line across box), uper and lower quartiles (box), range excluding outliers (whiskers) and outliers (points). The default setting for outliers is points more than 1.5xIQR above or below the quartiles.

*IQR = inter-quartile range.

import matplotlib.pyplot as plt
import numpy as np

x=np.random.randn(500) # samples from a normal distribution

plt.boxplot(x)

plt.show()

plot_011

Plotting groups

Boxplot can take data from multiple columns in a NumPy array.

x=np.random.randn(1000,5) # samples from a normal distribution

plt.boxplot(x)

plt.show()

plot_012

Or data may come from separate sources:

x1=list(np.random.randn(100)*5)
x2=list((np.random.randn(50)*2)+5)
x3=list(np.random.randn(250)*3)
x4=list((np.random.randn(70)*10)-10)

x=[x1,x2,x3,x4]

plt.boxplot(x)

plt.show()

plot_013

One thought on “42. Matplotlib: Boxplots

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