Multiple Box Plot in Python using Matplotlib

In this example, we are going to plot multiple box plots in a single figure?
By Anuj Singh Last updated : August 24, 2023

Multiple box plots

To multiple box plots using Matplotlib, you can use pyplot.boxplot() method which is a library method of the matplotlib library. This method is used to make a box and whisker plot for each column of x or each vector in sequence x.

Syntax

Consider the below syntax: [documentation reference]

matplotlib.pyplot.boxplot(
    x, notch=None, sym=None, 
    vert=None, whis=None, positions=None, 
    widths=None, patch_artist=None, 
    bootstrap=None, usermedians=None, conf_intervals=None, 
    meanline=None, showmeans=None, showcaps=None, 
    showbox=None, showfliers=None, boxprops=None, 
    labels=None, flierprops=None, medianprops=None, 
    meanprops=None, capprops=None, whiskerprops=None, 
    manage_ticks=True, autorange=False, zorder=None, *, 
    data=None)

Multiple box plots: Vertical version

To create a vertical version of multiple box plots, you can set the value of the vert parameter "True" in the pyplot.boxplot() method.

Example

Multiple Box Plot in Python (1)

Multiple box plots: Horizontal version

To create a horizontal version of multiple box plots, you can set the value of the vert parameter "False" in the pyplot.boxplot() method.

Example

Multiple Box Plot in Python (2)

Python program for multiple box plot using matplotlib

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(562201)
all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)]
labels = ['x1', 'x2', 'x3']

#MultipleBoxplot
plt.boxplot(all_data, vert=True, patch_artist=True, labels=labels) 
plt.ylabel('observed value')
plt.title('Multiple Box Plot : Vertical Version')
plt.show()

plt.boxplot(all_data, vert=False, patch_artist=True, labels=labels) 
plt.ylabel('observed value')
plt.title('Multiple Box Plot : Horizontal Version')  

Output

Output is as Figure

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.