English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Boxplot

The box plot, also known as the whisker plot, displays a summary of a set of data that includes the minimum value, the first quartile, the median, the third quartile, and the maximum value. In the box plot, a box is drawn from the first quartile to the third quartile. A vertical line passes through the middle of the box. The whiskers extend from each quartile to the minimum or maximum value.

Let's create data for the box plot. Use the numpy.random.normal() function to create dummy data. It requires three parameters: the mean and standard deviation of the normal distribution, and the number of values required.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)

The array list created above is the only input required to create boxplot. Use the data_to_plot code line and the following code to create a boxplot -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
fig = plt.figure()
# Create an axis instance
ax = fig.add_axes([0,0,1,1])
# Create boxplot
bp = ax.boxplot(data_to_plot)
plt.show()

Running the above code lines will generate the following output -