A Box Plot is also known as Whisker plot is created to display the
summary of the set of data values having properties like minimum, first
quartile, median, third quartile and maximum. In the box plot, a box is
created from the first quartile to the third quartile, a vertical line is also
there which goes through the box at the median. Here x-axis denotes the
data to be plotted while the y-axis shows the frequency distribution.
Creating Box Plot
The [Link] module of matplotlib library provides boxplot()
function with the help of which we can create box plots.
Syntax:
[Link](data, notch=None, vert=None, patch_artist=None,
widths=None)
Parameters:
Attribute Value
data array or sequence of array to be plotted
notch optional parameter accepts boolean values
optional parameter accepts boolean values false and true for
vert
horizontal and vertical plot respectively
optional parameter accepts int specifies intervals around notched
bootstrap
boxplots
optional parameter accepts array or sequence of array dimension
usermedians
compatible with data
positions optional parameter accepts array and sets the position of boxes
Attribute Value
widths optional parameter accepts array and sets the width of boxes
patch_artist optional parameter having boolean values
labels sequence of strings sets label for each dataset
optional having boolean value try to render meanline as full width of
meanline
box
order optional parameter sets the order of the boxplot
The data values given to the [Link]() method can be a Numpy array or
Python list or Tuple of arrays. Let us create the box plot by using
[Link]() to create some random data, it takes mean,
standard deviation, and the desired number of values as arguments.
Example:
• Python3
# Import libraries
import [Link] as plt
import numpy as np
# Creating dataset
[Link](10)
data = [Link](100, 20, 200)
fig = [Link](figsize =(10, 7))
# Creating plot
[Link](data)
# show plot
[Link]()
Output:
Customizing Box Plot
The [Link]() provides endless customization possibilities
to the box plot. The notch = True attribute creates the notch format to the
box plot, patch_artist = True fills the boxplot with colors, we can set
different colors to different [Link] vert = 0 attribute creates horizontal
box plot. labels takes same dimensions as the number data sets.
Example 1:
• Python3
# Import libraries
import [Link] as plt
import numpy as np
# Creating dataset
[Link](10)
data_1 = [Link](100, 10, 200)
data_2 = [Link](90, 20, 200)
data_3 = [Link](80, 30, 200)
data_4 = [Link](70, 40, 200)
data = [data_1, data_2, data_3, data_4]
fig = [Link](figsize =(10, 7))
# Creating axes instance
ax = fig.add_axes([0, 0, 1, 1])
# Creating plot
bp = [Link](data)
# show plot
[Link]()
Output:
Example 2: Let’s try to modify the above plot with some of the
customizations:
• Python3
# Import libraries
import [Link] as plt
import numpy as np
# Creating dataset
[Link](10)
data_1 = [Link](100, 10, 200)
data_2 = [Link](90, 20, 200)
data_3 = [Link](80, 30, 200)
data_4 = [Link](70, 40, 200)
data = [data_1, data_2, data_3, data_4]
fig = [Link](figsize =(10, 7))
ax = fig.add_subplot(111)
# Creating axes instance
bp = [Link](data, patch_artist = True,
notch ='True', vert = 0)
colors = ['#0000FF', '#00FF00',
'#FFFF00', '#FF00FF']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
# changing color and linewidth of
# whiskers
for whisker in bp['whiskers']:
[Link](color ='#8B008B',
linewidth = 1.5,
linestyle =":")
# changing color and linewidth of
# caps
for cap in bp['caps']:
[Link](color ='#8B008B',
linewidth = 2)
# changing color and linewidth of
# medians
for median in bp['medians']:
[Link](color ='red',
linewidth = 3)
# changing style of fliers
for flier in bp['fliers']:
[Link](marker ='D',
color ='#e7298a',
alpha = 0.5)
# x-axis labels
ax.set_yticklabels(['data_1', 'data_2',
'data_3', 'data_4'])
# Adding title
[Link]("Customized box plot")
# Removing top axes and right axes
# ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# show plot
[Link]()
Output: