0% found this document useful (0 votes)
28 views7 pages

Box Plot

The document discusses box plots, including how to create them in Python using matplotlib. It provides the boxplot() function syntax and parameters. It also gives examples of creating basic box plots with random data and more advanced box plots with options like fills, labels, notches, horizontal orientation, and custom box colors.

Uploaded by

mittasandhya13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Box Plot

The document discusses box plots, including how to create them in Python using matplotlib. It provides the boxplot() function syntax and parameters. It also gives examples of creating basic box plots with random data and more advanced box plots with options like fills, labels, notches, horizontal orientation, and custom box colors.

Uploaded by

mittasandhya13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Box Plot

A Box Ploti s 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 matplotlib.pyplot module of matplotlib library provides boxplot() function


with the help of which we can create box plots.
Syntax:

matplotlib.pyplot.boxplot(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 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)

data_1 = np.random.normal(100, 10, 200)


data_2 = np.random.normal(90, 20, 200)
data_3 = np.random.normal(80, 30, 200)
data_4 = np.random.normal(70, 40, 200)
data = [data_1, data_2, data_3, data_4]

fig = plt.figure(figsize =(10, 7))


# Creating axes instance
ax = fig.add_axes([0, 0, 1, 1])

# Creating plot
bp = ax.boxplot(data)

# show plot
plt.show()

Create box plot in python:

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]
plt.boxplot(box_plot_data)
plt.show()

Create box plot in python with fills and labels:

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]
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.

Create box plot in python with notch

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]
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'],
)

colors = ['cyan', 'lightblue', 'lightgreen', 'tan']


for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)

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.

You might also like