0
0
Matplotlibdata~5 mins

Box plot with plt.boxplot in Matplotlib

Choose your learning style9 modes available
Introduction

A box plot helps you see how data is spread out and where most values lie. It shows the middle, spread, and any unusual points.

To quickly check the spread and center of exam scores in a class.
To compare the distribution of sales numbers between different stores.
To find outliers in daily temperatures over a month.
To summarize the variation in customer ratings for a product.
Syntax
Matplotlib
plt.boxplot(data, notch=False, vert=True, patch_artist=False, showfliers=True)

data is your list or array of numbers.

notch=True makes the box plot show a notch around the median.

Examples
Basic box plot for a simple list of numbers.
Matplotlib
plt.boxplot([10, 20, 30, 40, 50])
Box plot with a notch and horizontal orientation.
Matplotlib
plt.boxplot(data, notch=True, vert=False)
Box plot with colored boxes and hiding outliers.
Matplotlib
plt.boxplot(data, patch_artist=True, showfliers=False)
Sample Program

This code creates a box plot of exam scores with a notch to highlight the median. The boxes are colored for better visibility.

Matplotlib
import matplotlib.pyplot as plt

# Sample data: exam scores
scores = [55, 70, 65, 90, 85, 75, 60, 95, 80, 100, 50, 45, 85, 70]

plt.boxplot(scores, notch=True, patch_artist=True)
plt.title('Box plot of Exam Scores')
plt.ylabel('Scores')
plt.show()
OutputSuccess
Important Notes

Outliers are points far from the rest and shown as dots by default.

You can customize colors and styles using extra parameters.

Summary

Box plots show data spread, median, and outliers clearly.

Use plt.boxplot() with your data list to create one.

Customize appearance with options like notch and patch_artist.