How to Easily Create Box and Whisker Plot in Python

Hi, I’m a data‑science hobbyist who loves turning raw numbers into pictures that tell the story in one glance. Today I’m walking you through the humble but super useful box and whisker plot. I’ll show you the code I actually run, one error that tripped me up the first time, and a few quick ways I stretch the snippet when a project asks for more.

The Code I Use

Matplotlib.pyplot as plt   # Main plotting library

# ➊ Sample data
data = [25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75]

# ➋ Custom colours
box_color = 'dodgerblue'
median_color = 'red'
whisker_color = 'green'
flier_color = 'purple'

# ➌ Draw the box‑and‑whisker plot
plt.boxplot(
data,
boxprops = {'color': box_color},
medianprops = {'color': median_color},
whiskerprops = {'color': whisker_color},
flierprops = {'markerfacecolor': flier_color}
)

# ➍ Labels + title
plt.xlabel('Data')
plt.ylabel('Value')
plt.title('Colored Box and Whisker Plot')

plt.show()

What each block does

  1. Sample data – I keep it small so you can eyeball whether the output makes sense.
  2. Custom colours – Dead‑simple way to brand the graphic.
  3. Plot callplt.boxplot() creates the five‑number summary box plus whiskers and fliers.
  4. Labelling – Never skip titles and axes; the reader should “get it” in one second.

A Sneaky Error I Hit

: float() argument must be a string or a real number

Why it happens

plt.boxplot() expects a clean list of numbers. One stray None or a string like '55' sneaks in, and Matplotlib fails while trying to turn that value into a float.

How I guard against it

= [float(x) for x in data]  # Raises its own error if anything is non‑numeric
plt.boxplot(cleaned, ...) # Same styling params as before

The list‑comprehension both validates and converts. If something can’t turn into float(), I find out right here instead of wrestling with a Matplotlib stack trace later.

Stretch Goals (Mini‑Projects)

PracticeWhy I like itOne‑line hint
Wrap the plot in a functionKeeps notebooks tidy and reusable.def make_boxplot(data, title='Boxplot', save_as=None): …
Compare multiple seriesSide‑by‑side boxes reveal differences fast.Pass a list of lists to plt.boxplot() and set labels=['A','B'].
Load from CSVReal‑world data lives in files, not literals.scores = pd.read_csv('scores.csv')['score']
Auto‑save for social mediaI post plots straight from Python.plt.savefig('plot.png', dpi=300, bbox_inches='tight')
Pastel style sheetSoft colours pop on busy feeds.plt.style.use('seaborn-v0_8-pastel')

All‑in‑One Utility Function

pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path

def make_boxplot(
data,
title: str = 'Boxplot',
x_label: str = 'Dataset',
y_label: str = 'Value',
colours: dict | None = None,
save_as: Path | None = None):
"""
Render a coloured box‑and‑whisker plot from any 1‑D numeric iterable.
"""

# Default colour palette
palette = {
'box': 'dodgerblue',
'median': 'red',
'whisker': 'green',
'flier': 'purple'
}
colours = colours or palette

# Validate & coerce
clean = [float(x) for x in data]

plt.style.use('seaborn-v0_8-pastel') # Soft gridlines
plt.boxplot(
clean,
boxprops = {'color': colours['box']},
medianprops = {'color': colours['median']},
whiskerprops = {'color': colours['whisker']},
flierprops = {'markerfacecolor': colours['flier']}
)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)

if save_as:
plt.savefig(save_as, dpi=300, bbox_inches='tight')
plt.show()
plt.close()

# Example – pull a 'scores' column from CSV and plot
df = pd.read_csv('sample_scores.csv')
make_boxplot(df['scores'], title='Student Scores', save_as=Path('scores_boxplot.png'))

The function into your own script, swap in any numeric series, and call it. The save_as argument dumps a ready‑to‑post PNG in one line.

Designing the Social Graphic

When I want the code and the finished plot in one shareable square, I:

  1. Create a 1080 × 1080 px canvas (Canva or Figma).
  2. Drop the syntax‑highlighted code cell at the top.
  3. Place the pastel boxplot right under it.
  4. Add a skinny right‑hand column with flat icons + handles:
    • Instagram → /Pythonclcoding
    • YouTube → /Pythoncoding
    • Twitter (𝕏) → /clcoding
  5. Leave lots of white space so the plot jumps out in crowded feeds.

Final Thought

I’m a big believer that small, repeatable snippets make data work fun instead of fiddly. A box and whisker plot is only five minutes of code, but the insight you getmmedian, spread, outliersmoften saves hours of guesswork.

Related blog posts