Box & Violin Plots
A box plot is a compact chart that summarizes a distribution with its median, quartiles, and outliers, while a violin plot wraps that same summary in a smoothed density curve to reveal the shape of the data.
Learn Box & Violin Plots in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll draw box plots for several groups at once, customize whiskers and outliers, switch to violin plots, and learn to read quartiles and spread at a glance.
A box plot turns a column of numbers into a five-number summary. The box spans the middle 50% of the data — from the first quartile (Q1) to the third quartile (Q3) — and the line inside the box marks the median . The whiskers reach out to the smallest and largest values that aren't considered outliers. Pass one array to ax.boxplot() to get started.
What you'll see: a single vertical box centered near 72. The orange line inside marks the median, the box edges sit around 65 and 79, and the whiskers stretch out to roughly 45 and 100. A few stray circles beyond the whiskers flag the unusual scores.
The real power of box plots shows up when you compare several groups. Pass a list of arrays and Matplotlib draws one box per array. Name each box with tick_labels . Two handy options: notch=True pinches the box around the median to hint at a confidence interval, and showfliers=False hides the outlier dots when they clutter the view.
What you'll see: three boxes side by side. South is the tallest box (largest spread) and sits highest, North is short and compact, and the pinched notches near each median make it easy to judge whether the typical values really differ between branches.
A box plot hides one thing: the shape of the distribution. Two groups can share the same median and quartiles yet look completely different — one bell-shaped, one with two peaks. A violin plot fixes this by drawing a mirrored density curve. Call ax.violinplot() and turn on showmedians=True to mark the center.
What you'll see: two violin shapes. The left one bulges in the middle like a single bell, while the right one pinches in the center and bulges at top and bottom — clearly showing two clusters that a box plot alone would have flattened into one box.
Replace each ___ to compare two teams with a box plot and hide the outliers.
You passed a flat array instead of a list of arrays. Wrap your groups in a list: ax.boxplot([a, b, c]) , not ax.boxplot(a, b, c) .
Newer Matplotlib renamed the argument. Use tick_labels=[...] for box plots instead of labels=[...] .
ax.violinplot() doesn't take labels. Set them yourself with ax.set_xticks([1, 2]) then ax.set_xticklabels([...]) .
Compare morning commute times across three transit modes with a horizontal box plot, then add a violin plot beside it on a second axis.
Lesson complete — you can summarize distributions!
You drew single and grouped box plots, read the median, quartiles, and outliers, tuned the notch and flier options, and switched to violin plots to expose the shape of your data.
🚀 Up next: Grouped & Stacked Bars — compare multiple series in one bar chart.
Practice quiz
Which method draws a box plot?
- ax.boxplot()
- ax.box()
- ax.whisker()
- ax.quartile()
Answer: ax.boxplot(). ax.boxplot() draws the five-number-summary box plot.
What does the line inside the box mark?
- The mean
- The median
- The maximum
- The first quartile
Answer: The median. The line inside the box marks the median (the middle value).
The box of a box plot spans which range?
- min to max
- mean plus or minus one standard deviation
- Q1 to Q3 (the interquartile range)
- the 5th to 95th percentile
Answer: Q1 to Q3 (the interquartile range). The box runs from the first quartile (Q1) to the third quartile (Q3).
How do you draw one box per group from several arrays?
- Call boxplot once per array
- Pass them as separate positional args
- Use ax.violinplot only
- Pass a list of arrays to ax.boxplot()
Answer: Pass a list of arrays to ax.boxplot(). ax.boxplot([a, b, c]) draws one box per array in the list.
Which argument hides the outlier dots in ax.boxplot()?
- showfliers=False
- notch=False
- outliers=None
- hideoutliers=True
Answer: showfliers=False. showfliers=False hides the individual outlier markers (fliers).
In Matplotlib 3.9+, which argument names each box?
- names
- tick_labels
- categories
- xlabels
Answer: tick_labels. tick_labels names each box (older versions used labels).
What does a violin plot add that a box plot does not show?
- The exact median value
- The number of points
- A smoothed density curve revealing shape
- The axis labels
Answer: A smoothed density curve revealing shape. A violin plot draws a mirrored density curve, exposing the distribution's shape.
Which method draws a violin plot?
- ax.density()
- ax.shape()
- ax.kde()
- ax.violinplot()
Answer: ax.violinplot(). ax.violinplot() draws violin plots.
What do the dots beyond the whiskers represent?
- Outliers (fliers) past 1.5x the IQR
- The medians
- The means
- Grid lines
Answer: Outliers (fliers) past 1.5x the IQR. Those fliers are outliers more than 1.5x the IQR beyond the box.
Since ax.violinplot() takes no labels argument, how do you name the violins?
- Pass labels= to violinplot
- set_xticks then set_xticklabels
- Use tick_labels=
- Add a legend automatically
Answer: set_xticks then set_xticklabels. Set positions with ax.set_xticks([1,2]) then ax.set_xticklabels([...]).