← Back to Pandas Course | Chapter 12: Visualization Basics | Lesson 4 of 6

Box plot

A box plot summarizes a distribution's median, quartiles and outliers in one compact picture.

In this page:

  1. Box plot
Syntax
python
df.plot.box()
df.boxplot(column="value_column", by="group_column")

Box plot

df.plot.box() or df.boxplot(by=...) draws the median line, the box from Q1 to Q3, whiskers, and dots for outliers. It compares distributions across columns or groups at a glance.

Note: Points beyond the whiskers are potential outliers.

Example: Box plot

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"a": [10, 12, 11, 13, 12, 40], "b": [20, 22, 21, 23, 22, 24]})
ax = df.plot.box()
print("boxes:", len(ax.lines) // 6 if len(ax.lines) >= 12 else len(ax.lines))
print(df.describe().loc[["min", "50%", "max"]])

# Output:
# boxes: 2
#         a     b
# min  10.0  20.0
# 50%  12.0  22.0
# max  40.0  24.0
Related Topics
Common Mistakes
  1. Reading the box line as the mean
  2. Ignoring the whiskers definition
  3. Using it on tiny samples
Chapter Summary
  • Box shows median and quartiles
  • Whiskers show spread
  • Dots mark outliers
  • Compare groups side by side
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.