0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Box Plot in Matplotlib: Simple Guide

To create a box plot in matplotlib, use the boxplot() function on your data array or list. This function visualizes data distribution by showing median, quartiles, and outliers in a simple plot.
๐Ÿ“

Syntax

The basic syntax to create a box plot is matplotlib.pyplot.boxplot(data, notch=False, vert=True, patch_artist=False).

  • data: The list or array of numbers to plot.
  • notch: If True, draws a notch to show confidence interval around the median.
  • vert: If True, the box plot is vertical; if False, horizontal.
  • patch_artist: If True, fills the box with color.
python
import matplotlib.pyplot as plt

data = [1, 2, 3, 4, 5]  # Example data
plt.boxplot(data, notch=False, vert=True, patch_artist=False)
plt.show()
๐Ÿ’ป

Example

This example shows how to create a simple vertical box plot with sample data. It demonstrates the median, quartiles, and outliers visually.

python
import matplotlib.pyplot as plt
import numpy as np

# Sample data: 100 random numbers from a normal distribution
np.random.seed(0)
data = np.random.normal(100, 20, 100)

plt.boxplot(data, notch=True, patch_artist=True)
plt.title('Box Plot Example')
plt.ylabel('Values')
plt.show()
Output
A vertical box plot showing the distribution of 100 random values with a notch around the median and colored box.
โš ๏ธ

Common Pitfalls

Common mistakes when creating box plots include:

  • Passing data in wrong format (must be list or array of numbers).
  • Not calling plt.show() to display the plot.
  • Confusing vertical and horizontal orientation by misusing vert parameter.
  • Ignoring outliers which can affect the scale of the plot.
python
import matplotlib.pyplot as plt

# Wrong: data as a single number instead of list
# plt.boxplot(5)  # This will cause an error

# Right way:
data = [5, 7, 8, 5, 6]
plt.boxplot(data)
plt.show()
Output
A vertical box plot showing the distribution of the small list of numbers.
๐Ÿ“Š

Quick Reference

Here is a quick summary of key boxplot() parameters:

ParameterDescriptionDefault
dataList or array of numbers to plotRequired
notchDraw notch to show confidence intervalFalse
vertPlot vertically if True, horizontally if FalseTrue
patch_artistFill box with color if TrueFalse
showmeansShow mean marker if TrueFalse
labelsLabels for each datasetNone
โœ…

Key Takeaways

Use plt.boxplot(data) to create a basic box plot in matplotlib.
Set notch=True to add a notch showing median confidence interval.
Always call plt.show() to display the plot.
Pass data as a list or array of numbers, not a single value.
Use patch_artist=True to fill the box with color for better visuals.