DEV Community

TianYi Zhang
TianYi Zhang

Posted on

What is a Box Plot? A Complete Guide

A box plot (also called a box-and-whisker plot) is a standardized way of displaying the distribution of a dataset based on a five-number summary: minimum, first quartile (Q1), median, third quartile (Q3), and maximum. Invented by statistician John Tukey, the plot gets its name from the rectangular "box" spanning the interquartile range, with "whiskers" extending outward like a cat's whiskers.


Key Terms

Term Definition
Minimum (Min) The smallest data point excluding outliers
First Quartile (Q1) The median of the lower half. 25% of data falls below Q1
Median (Q2) The middle value. 50% of data falls below the median
Third Quartile (Q3) The median of the upper half. 75% of data falls below Q3
Maximum (Max) The largest data point excluding outliers
Interquartile Range (IQR) Q3 − Q1. The width of the box, measuring the spread of the middle 50% of data
Mean The arithmetic average of all data points
Outliers Data points beyond Q1 − 1.5×IQR or Q3 + 1.5×IQR

Why Use Box Plots?

  • Compare groups instantly. Place boxes side by side to see medians, spreads, and outliers across categories at a glance.
  • Detect skewness visually. A lopsided box reveals asymmetry — a long upper whisker indicates right skew, a long lower whisker indicates left skew.
  • Identify outliers objectively. The IQR-based Tukey fences method flags statistically unusual data points.
  • Summarize large datasets compactly. One box plot can represent the distribution of thousands of data points.

Worked Example

Let's walk through a real example using exam scores. Suppose we have scores for four subjects, each with 12 students:

Math:      72, 85, 78, 90, 65, 88, 76, 92, 70, 84, 79, 86
Physics:   68, 75, 82, 70, 88, 74, 80, 66, 85, 73, 90, 72
Chemistry: 60, 68, 72, 58, 75, 70, 65, 80, 62, 74, 68, 67
Biology:   78, 85, 82, 90, 88, 84, 86, 92, 80, 87, 83, 89
Enter fullscreen mode Exit fullscreen mode

We'll use the Math scores to demonstrate how each key term is calculated step by step.

Step 1: Sort the Data

First, sort all values in ascending order:

Sorted: 65, 70, 72, 76, 78, 79, 84, 85, 86, 88, 90, 92
Enter fullscreen mode Exit fullscreen mode

Step 2: Calculate Quartiles (Linear Interpolation)

We use linear interpolation (Hyndman & Fan method 7) to compute percentiles — the same method used by Python's NumPy and pandas.

Core Idea

With 12 sorted data points, the "25th percentile position" may not fall exactly on a data point — it can land between two values. Linear interpolation takes the value proportionally between the two surrounding data points.

Computing Q1 (25th percentile)

Sorted data with indices:

Index:  0   1   2   3   4   5   6   7   8   9   10  11
Value: 65  70  72  76  78  79  84  85  86  88  90  92
Enter fullscreen mode Exit fullscreen mode

Calculate the position for Q1:

The 12 data points occupy positions 0 through 11. The 25th percentile position is:

position = (12 - 1) × 0.25 = 2.75
Enter fullscreen mode Exit fullscreen mode

Position 2.75 means we are between index 2 (value 72) and index 3 (value 76), at 75% of the distance from index 2.

Interpolate proportionally:

Q1 = 72 + 0.75 × (76 - 72)
   = 72 + 0.75 × 4
   = 72 + 3
   = 75
Enter fullscreen mode Exit fullscreen mode

Visual diagram:

Index:     2              2.75             3
Value:    72 ──────────────●────────────── 76
                           ↑
                    75% of the way
                    72 + 0.75×4 = 75
Enter fullscreen mode Exit fullscreen mode

Result: Q1 = 75

This means 25% of students scored below 75 in Math.

Computing the Median Q2 (50th percentile)

position = (12 - 1) × 0.5 = 5.5
lo = 5  →  sorted[5] = 79
hi = 6  →  sorted[6] = 84
fraction = 5.5 - 5 = 0.5

Median = 79 + 0.5 × (84 - 79) = 79 + 2.5 = 81.5
Enter fullscreen mode Exit fullscreen mode

Result: Median = 81.5

Half the students scored below 81.5, half above.

Computing Q3 (75th percentile)

position = (12 - 1) × 0.75 = 8.25
lo = 8  →  sorted[8] = 86
hi = 9  →  sorted[9] = 88
fraction = 8.25 - 8 = 0.25

Q3 = 86 + 0.25 × (88 - 86) = 86 + 0.5 = 86.5
Enter fullscreen mode Exit fullscreen mode

Result: Q3 = 86.5

75% of students scored below 86.5.

Step 3: Calculate the Interquartile Range (IQR)

IQR = Q3 − Q1 = 86.5 − 75 = 11.5
Enter fullscreen mode Exit fullscreen mode

Result: IQR = 11.5

The middle 50% of students' scores span 11.5 points. A larger IQR indicates greater spread in the data.

Step 4: Calculate Outlier Fences (Tukey's Fences)

Tukey's fences method uses 1.5 × IQR as the threshold for flagging outliers:

lowerFence = 75 − 1.5 × 11.5 = 75 − 17.25 = 57.75
upperFence = 86.5 + 1.5 × 11.5 = 86.5 + 17.25 = 103.75
Enter fullscreen mode Exit fullscreen mode

Check the original data for values below 57.75 or above 103.75:

Original: 72, 85, 78, 90, 65, 88, 76, 92, 70, 84, 79, 86
All values are within [57.75, 103.75]
Enter fullscreen mode Exit fullscreen mode

Result: No outliers.

These Math scores are fairly concentrated — no extreme high or low values.

Step 5: Determine Whisker Endpoints (Min and Max)

The whiskers extend to the smallest and largest non-outlier values:

Since there are no outliers, the whiskers reach directly to the data extremes:

Min = 65  (first sorted value)
Max = 92  (last sorted value)
Enter fullscreen mode Exit fullscreen mode

Step 6: Calculate the Mean

Sum = 65 + 70 + 72 + 76 + 78 + 79 + 84 + 85 + 86 + 88 + 90 + 92 = 965
Mean = 965 ÷ 12 ≈ 80.42
Enter fullscreen mode Exit fullscreen mode

Result: Mean ≈ 80.42

Note that the mean (80.42) is slightly lower than the median (81.5), indicating a mild left skew — a few lower scores pull the average down.


Complete Calculation Code

Here is all the logic consolidated into a single function:

function calculateBoxPlotStats(data, iqrMultiplier = 1.5) {
  // 1. Sort
  const sorted = [...data].sort((a, b) => a - b);
  const n = sorted.length;

  // 2. Percentile via linear interpolation
  function percentile(p) {
    const h = (n - 1) * p;
    const lo = Math.floor(h);
    const hi = Math.ceil(h);
    if (lo === hi) return sorted[lo];
    return sorted[lo] + (h - lo) * (sorted[hi] - sorted[lo]);
  }

  // 3. Five-number summary
  const q1 = percentile(0.25);
  const median = percentile(0.5);
  const q3 = percentile(0.75);
  const iqr = q3 - q1;

  // 4. Outlier fences
  const lowerFence = q1 - iqrMultiplier * iqr;
  const upperFence = q3 + iqrMultiplier * iqr;

  // 5. Mean
  const mean = sorted.reduce((sum, v) => sum + v, 0) / n;

  // 6. Outliers & whisker endpoints
  const outliers = sorted.filter(v => v < lowerFence || v > upperFence);
  const nonOutliers = sorted.filter(v => v >= lowerFence && v <= upperFence);
  const min = nonOutliers[0];
  const max = nonOutliers[nonOutliers.length - 1];

  return { min, q1, median, q3, max, iqr, mean, lowerFence, upperFence, outliers };
}

// Example usage
const mathScores = [72, 85, 78, 90, 65, 88, 76, 92, 70, 84, 79, 86];
const stats = calculateBoxPlotStats(mathScores);

console.log(stats);
// {
//   min: 65,
//   q1: 75,
//   median: 81.5,
//   q3: 86.5,
//   max: 92,
//   iqr: 11.5,
//   mean: 80.42,
//   lowerFence: 57.75,
//   upperFence: 103.75,
//   outliers: []
// }
Enter fullscreen mode Exit fullscreen mode

Interpreting the Result

When you plot all four subjects side by side, the comparison reveals clear patterns: Biology scores are consistently high and tightly clustered, while Chemistry scores are lower overall but evenly spread. This kind of multi-group comparison is exactly what makes box plots so powerful for exploratory data analysis.


Box Plot Result

The chart was generated by https://aiboxplot.com


Box Plot vs Other Chart Types

Chart Best For Limitation
Box Plot Comparing distributions, spotting outliers Hides multi-modal patterns
Violin Plot Showing distribution shape and density Less familiar to general audiences
Histogram Revealing frequency and modality Bin width choices affect appearance
Scatter Plot Exploring XY relationships Needs two numeric variables

Computation Method

  • Quartiles: Linear interpolation (Hyndman & Fan method 7), consistent with Python's NumPy and pandas.
  • Outliers: Tukey fences — lower fence = Q1 − 1.5×IQR, upper fence = Q3 + 1.5×IQR.
  • Notched boxes: 95% CI around median (±1.58 × IQR / √n), enabling visual significance testing.

Frequently Asked Questions

What is the minimum number of data points needed?

You need at least 5 data points for a box plot. For reliable outlier detection and stable quartile estimates, 20+ points per dataset is recommended.

How accurate is the outlier detection?

The tool uses the standard Tukey fences method (1.5 × IQR), the industry standard taught in statistics courses and used in research. You can adjust sensitivity to 2× or 3× IQR for more conservative detection.

Is my data stored or shared?

No. All computation happens locally in your browser. Data is never uploaded to any server, stored, or shared. This tool is completely private.

Can I use exported charts in academic papers?

Yes. SVG exports are vector graphics that scale to any resolution — ideal for publications. PNG exports at 2x resolution for sharp rendering in presentations.

Does this tool work on mobile devices?

Yes. The interface is fully responsive and works on phones, tablets, and desktops. The chart canvas adapts to screen size.

Is this tool really free?

Yes, completely free. No sign-up required, no ads, no watermarks. All statistical tools are available with full functionality at zero cost.


References

  • Hyndman, R. J. & Fan, Y. (1996). "Sample Quantiles in Statistical Packages." The American Statistician, 50(4), 361–365.
  • Tukey, J. W. (1977). Exploratory Data Analysis. Addison-Wesley.

Top comments (0)