#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Box Plots

Learn to show data spread and outliers visually

Box Plots

What is a Box Plot?

A box plot shows 5 key numbers at once:

  • Minimum
  • 25th percentile (Q1)
  • Median (50th percentile)
  • 75th percentile (Q3)
  • Maximum

Plus it shows outliers as dots!

Basic Box Plot

code.py
import matplotlib.pyplot as plt

data = [20, 25, 28, 30, 32, 35, 38, 40, 45, 50, 80]

fig, ax = plt.subplots()
ax.boxplot(data)
ax.set_title('Box Plot')
plt.show()

Reading a Box Plot

| <- Maximum (or upper whisker) | ----- <- Q3 (75%) | | | - | <- Median (50%) | | ----- <- Q1 (25%) | | <- Minimum (or lower whisker) o <- Outliers (dots outside)

Compare Multiple Groups

code.py
import matplotlib.pyplot as plt

dept_A = [40, 45, 50, 55, 60, 65, 70]
dept_B = [50, 55, 60, 65, 70, 75, 80]
dept_C = [35, 40, 45, 50, 55, 60, 100]  # has outlier

fig, ax = plt.subplots()
ax.boxplot([dept_A, dept_B, dept_C])
ax.set_xticklabels(['Dept A', 'Dept B', 'Dept C'])
ax.set_ylabel('Salary (K)')
ax.set_title('Salary by Department')
plt.show()

Horizontal Box Plot

code.py
ax.boxplot(data, vert=False)

Show Mean

code.py
ax.boxplot(data, showmeans=True)

Mean shown as green triangle.

Change Colors

code.py
bp = ax.boxplot(data, patch_artist=True)

# Color the box
for box in bp['boxes']:
    box.set_facecolor('lightblue')

Hide Outliers

code.py
ax.boxplot(data, showfliers=False)

Notched Box Plot

Shows confidence interval around median:

code.py
ax.boxplot(data, notch=True)

Complete Example

code.py
import matplotlib.pyplot as plt
import numpy as np

# Sample data
np.random.seed(42)
group1 = np.random.normal(50, 10, 100)
group2 = np.random.normal(60, 15, 100)
group3 = np.random.normal(55, 8, 100)

fig, ax = plt.subplots(figsize=(10, 6))

bp = ax.boxplot([group1, group2, group3],
                patch_artist=True,
                showmeans=True)

# Color each box differently
colors = ['lightblue', 'lightgreen', 'lightyellow']
for patch, color in zip(bp['boxes'], colors):
    patch.set_facecolor(color)

ax.set_xticklabels(['Group A', 'Group B', 'Group C'])
ax.set_ylabel('Score')
ax.set_title('Score Distribution by Group')
ax.grid(True, alpha=0.3)

plt.show()

When to Use Box Plots?

  • Compare distributions across groups
  • Spot outliers quickly
  • See median and spread at a glance
  • Better than histogram for multiple groups

Key Points

  • Box shows Q1 to Q3 (middle 50%)
  • Line in box = median
  • Whiskers show range
  • Dots outside = outliers
  • patch_artist=True to color boxes
  • showmeans=True to add mean marker

What's Next?

Learn to customize your plots with colors, fonts, and styles.