Signup/Sign In

Matplotlib Box Plot - boxplot() Function

In this tutorial, we will cover about Box plot and creation of Box plot in the matplotlib Library using the boxplot() function.

The box plot in matplotlib is mainly used to displays a summary of a set of data having properties like minimum, first quartile, median, third quartile, and maximum.

  • The Box Plot is also known as Whisker Plot.

  • The box is created from the first quartile to the third quartile in the box plot, also there is a verticle line going through the box at the median.

  • In the Box Plot, the x-axis indicates the data to be plotted while the y-axis denotes the frequency distribution.

Creating the Box Plot

The Box plot in the matplotlib library is usually created with the help of boxplot() function.

  • In the Box Plot the numpy.random.normal() is used to create some random data, it takes mean, standard deviation, and the desired number of values as its arguments.

  • The provided data values to the ax.boxplot() method can be a Numpy array or Python list or it can be Tuple of arrays

The required syntax for the boxplot() function is as follows:

matplotlib.pyplot.boxplot(data, notch, vert, patch_artist, widths)

Following are the parameters of this function:

  • data

    This parameter indicates the array or sequence of arrays needed to plot.

  • notch

    This is an optional parameter that accepts boolean values. It has None as default value.

  • vert

    This is an optional parameter that accepts boolean values that is false for horizontal plot and true for vertical plot respectively.

  • patch_artist

    This is an optional parameter having boolean value with None as its default value

  • widths

    This is an optional parameter that accepts an array and used to set the width of boxes. The default value is None.

Now we will dive into some examples of creating a Box plot.

Creating a Box Plot Example:

The code for creating a simple Box plot in the Matplotlib library is as follows:

import matplotlib.pyplot as plt
 
value1 = [84,77,20,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52]
value2=[62,5,91,25,35,32,96,99,3,90,95,34,27,55,100,15,71,11,37,21]
value3=[23,89,12,78,72,89,25,69,68,86,19,48,15,16,16,75,65,31,25,52]
value4=[59,73,73,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
 
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data)
plt.show()

Here is the output:

box plot matplotlib example

Creating a Box plot with Fills and Labels:

In the code snippet given below, we will provide a label to the box plot and will fill the box plot. Let us see the code for the example:

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,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
 
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,patch_artist=True,labels=['subject1','subject2','subject3','subject4'])
plt.show()

Here is the output:

box plot matplotlib example

Creating a Box plot with Notch:

In this example, we will plot a box plot having a notch.

import matplotlib.pyplot as plt
 
value1 = [84,76,24,46,67,62,78,78,71,38,98,89,78,69,72,82,87,68,56,59]
value2=[62,5,91,25,39,32,96,99,3,98,95,32,27,55,100,15,71,11,37,29]
value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52]
value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30]
 
box_plot_data=[value1,value2,value3,value4]
plt.boxplot(box_plot_data,notch='True',patch_artist=True,labels=['subject1','subject2','subject3','subject4'])
plt.show()

Here is the output:

box plot matplotlib example

Time For Live Example!

In this live example, we will draw a horizontal box plot having different colors.

Explanation of the code

  • In the above example, the boxplot() function takes argument vert=0 because we want to plot the horizontal box plot.

  • The colors array in the above example will take up four different colors and passed to four different boxes of the boxplot with the help of patch.set_facecolor() function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.