Matplotlib Box Plot – 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:

  • dataThis parameter indicates the array or sequence of arrays needed to plot.
  • notchThis is an optional parameter that accepts boolean values. It has None as default value.
  • vertThis is an optional parameter that accepts boolean values that is false for horizontal plot and true for vertical plot respectively.
  • patch_artistThis is an optional parameter having boolean value with None as its default value
  • widthsThis 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.

Follow Us On