Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 57 ยท Core Chart Types

Box Plots and Violin Plots

Draw and read box plots and violin plots in Matplotlib: quartiles, whiskers, fliers, notches, grouped boxes, and when each chart can mislead.

A histogram shows one distribution well, but it is hard to compare five of them side by side. Box plots and violin plots solve that: each group becomes a compact shape, and you line the shapes up on one axis. They are the standard tools for questions such as "do the three server types differ in response time?"

This lesson explains what every part of a box plot means, how to group and style boxes, how a violin plot adds shape information, and when each chart can mislead you. If histograms are new, read Histograms and Distributions first.

Anatomy of a box

The box Spans Q1 to Q3, the middle half of the data.
The median A line inside the box.
Whiskers Reach the furthest values within 1.5 x IQR.
Fliers Dots beyond the whiskers.

IQR is the interquartile range, Q3 minus Q1.

1 / 2

Reading a box plot

For each group, a box plot draws the five-number summary plus outliers:

  • The box runs from the first quartile (Q1, 25 percent of values lie below) to the third quartile (Q3, 75 percent below).
  • A line inside marks the median, the middle value.
  • Whiskers extend from the box to the most extreme data points that lie within 1.5 times the box height (the interquartile range, IQR) from the box edges.
  • Fliers are points beyond the whiskers, drawn individually. They are candidates for outliers, not proof of errors.

The width of the box means nothing. Only the vertical positions carry information.

Python A box plot and its numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

ax.boxplot returns a dictionary of the artists it created (boxes, medians, whiskers, caps, fliers, means), which is how you restyle each part. Compare the printed whisker limits with the picture: every point beyond them is a flier.

Several groups and styling

Pass a list of arrays (or a 2D array, one column per box) to get one box per group. Use tick_labels= to name them (older tutorials use labels=, which Matplotlib 3.9 renamed). Useful options:

  • notch=True draws a notch around the median showing an approximate 95 percent confidence interval. If the notches of two boxes do not overlap, the medians likely differ.
  • patch_artist=True lets you fill the boxes with colors.
  • showmeans=True adds a marker at the mean.
  • vert=False lays the boxes horizontally.
  • showfliers=False hides the outliers (say so in the caption if you do).
Python Comparing three groups with notches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how quickly you see that the typical response time drops from small to large servers, and that the large group is also the most spread out (the tallest box). Four numbers per group, one glance.

Violin plots

A box plot hides the shape. Two very different distributions, one with a single peak and one with two separate peaks, can have the same quartiles. A violin plot restores the shape: it draws a mirrored kernel density estimate (a smoothed histogram) for each group.

ax.violinplot(datasets, showmedians=True) returns a dictionary of artists; the filled bodies live in parts["bodies"]. Options include showmeans, showextrema, widths and vert.

Python Same box, different shape

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The boxes look alike (similar middle and spread), yet the violin on the right reveals that the second group is two separate populations with almost nobody in the middle. This is the main reason to prefer a violin, or a histogram, when you suspect the data has more than one cluster.

When each chart misleads

Box plots

  • They hide multimodality, as you just saw.
  • With very few points (say under 10), the box suggests more certainty than the data supports. Overlay the raw points, for example with a jittered ax.scatter, or use a strip plot.
  • Fliers are defined by a rule, not by truth. A flier can be a typo or the most interesting observation.

Violin plots

  • The smooth outline comes from a bandwidth choice. A small sample or a wide bandwidth can invent bumps or smear real ones.
  • The density tails extend beyond the data range by default, so a violin can show values that never occurred (like negative prices). Use ax.violinplot(..., bw_method=...) carefully and check the extremes.
  • Readers unfamiliar with violins sometimes read width as the number of observations. Width is relative density within each group, so groups of different sizes look equally "full".

A good habit is to show the raw points on top when the data set is small, and to state the sample size in the label (for example "large (n=150)").

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In ax.boxplot, what do the whiskers extend to by default?

    Whiskers stop at the most extreme data point within 1.5 x IQR of the box. Points beyond them are drawn as fliers.
  2. 2. What does the width of a box in a box plot represent?

    Box width is a layout choice. The notch option is the part that shows a confidence interval for the median.
  3. 3. Which keyword argument names the boxes in Matplotlib 3.10?

    tick_labels replaced the old labels argument of boxplot in Matplotlib 3.9.
  4. 4. Why can a violin plot reveal something a box plot hides?

    A violin draws a smoothed density, so bimodal data shows two bulges, while a box only summarizes quartiles.

Summary

  • A box plot shows the median, the quartiles (the box), whiskers within 1.5 x IQR and individual fliers.
  • ax.boxplot([a, b, c], tick_labels=[...]) draws grouped boxes; notch, patch_artist, showmeans and vert customize them, and the returned dictionary lets you style each part.
  • ax.violinplot draws a mirrored density per group, so it shows peaks that box plots hide.
  • Box plots hide multimodality and look too certain with small samples; violins depend on a smoothing bandwidth and can extend past the data range.
  • Show raw points and the sample size when you can.

Next, learn when to use parts of a whole (and when not to) in Pie, Donut and Stacked Area Charts.