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]

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]

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]

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.