Box Plot
Box Plot
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 horizontal and
vert
vertical plot respectively
bootstrap optional parameter accepts int specifies intervals around notched boxplots
usermedians optional parameter accepts array or sequence of array dimension compatible with
Attribute Value
data
positions optional parameter accepts array and sets the position of boxes
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
meanline optional having boolean value try to render meanline as full width of box
order optional parameter sets the order of the boxplot
The data values given to the ax.boxplot() method can be a Numpy array or
Python list or Tuple of arrays. Let us create the box plot by using
numpy.random.normal() to create some random data, it takes mean, standard
deviation, and the desired number of values as arguments.
Example:
# Import libraries Output:[1, 2, 3, 4]
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
data = [1,2,3,4,]
print(data)
fig = plt.figure(figsize =(10, 5))
# Creating plot
plt.boxplot(data)
# show plot
plt.show()
Example2:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 6), columns=['A', 'B', 'C', 'D', 'E','F'])
print(df)
df.plot.box()
Output:
A B C D E F
0 0.943823 0.948426 0.254431 0.908384 0.471026 0.208383
1 0.410148 0.583931 0.888999 0.903374 0.559775 0.807161
2 0.529115 0.095077 0.435739 0.699166 0.754548 0.677854
3 0.002105 0.682337 0.980021 0.213004 0.713217 0.197689
4 0.766489 0.935797 0.208201 0.829341 0.299254 0.880705
5 0.031775 0.755231 0.215362 0.366887 0.157709 0.807431
6 0.201054 0.354579 0.365598 0.267523 0.223923 0.747610
7 0.577383 0.773863 0.156956 0.545064 0.079705 0.098411
8 0.431972 0.592250 0.716642 0.568289 0.077051 0.376896
9 0.737927 0.950551 0.905327 0.491590 0.935526 0.251383
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
np.random.seed(10)
# Creating plot
bp = ax.boxplot(data)
# show plot
plt.show()
value1 =
[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21
]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,5
2]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,3
0]
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data)
plt.show()
value1 =
[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21
]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,5
2]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,3
0]
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,patch_artist=True,labels=['course1','course2
','course3','course4'])
plt.show()
boxplot() function takes the data array to be plotted as input in first argument,
second argument patch_artist=True , fills the boxplot and third argument takes the
label to be plotted.
value1 =
[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21
]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,5
2]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,3
0]
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,notch='True',patch_artist=True,labels=['cour
se1','course2','course3','course4'])
plt.show()
Horizontal box plot in python with different colors:
import matplotlib.pyplot as plt
value1 =
[82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21
]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,5
2]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,3
0]
box_plot_data=[value1,value2,value3,value4]
box=plt.boxplot(box_plot_data,vert=0,patch_artist=True,labels=['cour
se1','course2','course3','course4'],
)
plt.show()
•boxplot() function takes argument vert =0 which plots the horizontal box plot.
•Colors array takes up four different colors and passed to four different boxes of
the boxplot with patch.set_facecolor() function.