Matplotlib: Box and Whisker Plot

This recipe includes the following topics:

  • Draw a Box and Whisker Plot for single column
  • Draw a Box and Whisker Plot for all columns


# import module
import pandas as pd
import matplotlib.pyplot as plt

fileGitURL = 'https://raw.githubusercontent.com/andrewgurung/data-repository/master/pima-indians-diabetes.data.csv'

# define column names
cols = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']

# load file as a Pandas DataFrame
pimaDf = pd.read_csv(fileGitURL, names=cols)

# draw box plot for single column
pimaDf['mass'].plot.box()

# draw box plot for all columns
# subplots=True: creates a subplot for each column
# sharey=False: creates separate y-axis range for each column
pimaDf.plot.box(subplots=True, layout=(3,3), figsize=(15,15), sharey=False)

plt.show()
<img src="http://www.andrewgurung.com/wp-content/uploads/2018/12/matplotlib-box1.png" alt="Box and whisker plot for 'mass' column" width="372" height="252" class="size-full wp-image-494" /> Fig: Box and whisker plot for 'mass' column

<img src="http://www.andrewgurung.com/wp-content/uploads/2018/12/matplotlib-box2.png" alt="Box and whisker plot for all columns" width="884" height="850" class="size-full wp-image-495" /> Fig: Box and whisker plot for all columns

Leave a Reply

Your email address will not be published. Required fields are marked *