logo

How to Draw a Box Plot in Python matplotlib 📂Programing

How to Draw a Box Plot in Python matplotlib

Description

plt.boxplot() can draw a Box Plot. If you draw it with the default settings, it appears as a white box with black lines only, so if you want to make it prettier, you have to manually adjust the settings.

Code

Basic

import numpy as np
import matplotlib.pyplot as plt

x = 100*np.random.random_sample(100)
y = 50*np.random.random_sample(100) + 50
z = np.concatenate((x,y))

plt.boxplot([x,y,z])
plt.show()

Legend

fig, ax = plt.subplots()

bp1 = ax.boxplot(x, positions=[1])
bp2 = ax.boxplot(y, positions=[2])
bp3 = ax.boxplot(z, positions=[3])

ax.legend([bp1["boxes"][0], bp2["boxes"][0], bp3["boxes"][0]], ['x', 'y', 'z'], loc='upper right')
plt.show()

Color

To apply the following settings, you need to set patch_artist=True in the boxplot options. The options are as follows:

  • boxprops=dict(facecolor='',color=''): Box style
    • facecolor: Inner color of the box
    • color: Border color of the box
  • capprops = dict(color=''): Color of the top (bottom) horizontal line
  • whiskerprops = dict(color=''): Color of the whiskers (lines extending from the box)
  • medianprops = dict(color=''): Color of the horizontal line representing the median
  • meanprops = dict(marker='o', markerfacecolor='', markeredgecolor=''): Style for the mean
    • marker: Shape of the marker
    • markerfacecolor: Inner color of the marker
    • markeredgecolor: Border color of the marker
fig, ax = plt.subplots()

bp1 = ax.boxplot(x, positions=[1], showmean=True, patch_artist = True,
                                                  boxprops     = dict(facecolor='pink',color='red'),
                                                  capprops     = dict(color='red'),
                                                  whiskerprops = dict(color='red'),
                                                  medianprops  = dict(color='red'),
                                                  meanprops    = dict(marker='o',markerfacecolor='red', markeredgecolor='red'))

bp2 = ax.boxplot(y, positions=[2], showmean=True, patch_artist = True, 
                                                  boxprops     = dict(facecolor='lightblue',color='blue'),
                                                  capprops     = dict(color='blue'),
                                                  whiskerprops = dict(color='blue'),
                                                  medianprops  = dict(color='blue'),
                                                  meanprops    = dict(marker='o',markerfacecolor='blue', markeredgecolor='blue'))
                                   
bp3 = ax.boxplot(z, positions=[3], showmean=True, patch_artist = True,
                                                  boxprops     = dict(facecolor='lightgreen',color='green'),
                                                  capprops     = dict(color='green'),
                                                  whiskerprops = dict(color='green'),
                                                  medianprops  = dict(color='green'),
                                                  meanprops    = dict(marker='o',markerfacecolor='green', markeredgecolor='green'))

ax.legend([bp1["boxes"][0], bp2["boxes"][0], bp3["boxes"][0]], ['x', 'y', 'z'], loc='upper right')
plt.show()

Environment

  • OS: Windows11
  • Version: Python 3.9.13, matplotlib==3.6.2, numpy==1.23.5

See Also