Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท Statistical Estimation

Error Bars and Confidence Intervals in Seaborn

Understand the errorbar parameter in seaborn: confidence intervals, bootstrapping, standard deviation, standard error, percentile intervals and custom functions.

Many seaborn plots do not draw your raw data. A bar plot draws the mean of each group; a line plot draws the mean at each x; a regression plot draws a fitted line. Each of these is an estimate from a sample, and an honest chart shows how uncertain that estimate is. That is what the error bars and shaded bands are for.

Since version 0.12, one parameter controls all of this across plots: errorbar. This lesson explains what each setting means, how seaborn computes it, and how to choose.

Estimates and uncertainty

Suppose you measure the delivery time of 15 orders and the mean is 31 minutes. If you measured another 15 orders, you would get a slightly different mean. The confidence interval (CI) is a range built to capture the true mean most of the time (95% of the time, for a 95% interval) if you repeated the study. A wide interval means you do not know the mean precisely; a narrow one means you do.

Four things an error bar can mean

Confidence interval Where the true mean probably lies.
Standard error SD divided by sqrt(n). Precision of the mean.
Standard deviation How spread out the data itself is.
Percentile interval The middle slice of the data.

Always state which one your chart uses.

1 / 2

The errorbar parameter

errorbar accepts a name, or a (name, level) tuple, or a function:

Value Draws Notes
("ci", 95) or "ci" 95% confidence interval of the mean Default. Bootstrapped.
("ci", 99) 99% interval Wider
("se", 2) Mean plus/minus 2 standard errors Multiplier optional (default 1)
("sd", 1) Mean plus/minus 1 standard deviation Spread of the data
("pi", 50) Range containing the middle 50% of data Percentile interval
None Nothing
callable Your own function Must return (low, high)

It works in barplot, pointplot, lineplot and relplot(kind="line"). The older ci= and ci="sd" arguments are deprecated: use errorbar=.

Python Same data, four kinds of error bars

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 same bars get very different whiskers. The sd and pi whiskers are long because they describe the data. The ci and se whiskers are short because they describe only where the mean is. Both are right; they answer different questions. If you are comparing group means, show the CI or SE. If you want readers to see how variable individual cases are, show SD or a percentile interval, or better still, the raw points.

How the bootstrap works

For "ci", seaborn does not use a formula. It uses the bootstrap:

  1. Resample your data with replacement, giving a new sample of the same size.
  2. Compute the mean of that resample.
  3. Repeat 1,000 times (n_boot).
  4. Take the 2.5th and 97.5th percentiles of those means as the 95% interval.

It makes few assumptions about the shape of your data, and it works for any estimator, not just means. The cost is that it is random: you get slightly different intervals each time, unless you fix seed. See it by hand:

Python A bootstrap interval by hand

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 two intervals agree closely here. For skewed or small samples they can differ, and the bootstrap is usually the safer choice.

Control the bootstrap with seed and n_boot in the plotting function: sns.barplot(..., seed=42, n_boot=5000). Fix the seed for reproducible figures.

Bands on line plots

On a lineplot, each x value with several y observations gets its own mean, and the band shows the interval around that mean. Use err_style="bars" for whiskers instead of a band:

Python Band, bars and standard deviation on a line plot

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 SD band covers most of the raw points; the CI band is a thin ribbon around the trend. Many readers mistake one for the other, so say what the band means in the caption.

Custom error bars

A function that takes a Series and returns (low, high) becomes the error bar. Here is the interquartile range, and a "min to max" range:

Python Custom errorbar functions

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.

Note estimator="median" changes the bar height too. Estimators get their own lesson: estimators and aggregation.

Choosing wisely

  • Comparing group means: CI or SE, with enough observations (say 10+ per group). With a handful of points, plot the points.
  • Describing variation: SD or percentile interval, or show the distribution (box, violin, strip).
  • Skewed data: use a bootstrapped CI of the median or a percentile interval.
  • Do not compare overlapping CIs as a test. Overlap does not prove "no difference", and non-overlap is not a formal test either. Use a proper test, e.g. from SciPy.
  • Say what it is. "Error bars show 95% bootstrap CIs, n = 25 per group."

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the default errorbar in seaborn show?

    The default is ('ci', 95), computed by bootstrapping 1,000 resamples.
  2. 2. Which errorbar setting describes the spread of the data itself?

    sd is the standard deviation of the observations. ci and se describe the precision of the mean.
  3. 3. How do you make bootstrapped intervals reproducible?

    seed fixes the random number generator used for resampling.
  4. 4. What must a custom errorbar function return?

    The function receives the observations and returns the lower and upper limits.

Summary

  • errorbar replaces ci: use ("ci", 95), ("se", k), ("sd", k), ("pi", p), None or a function.
  • CI and SE measure how well you know the mean; SD and PI measure spread.
  • The CI is bootstrapped; control it with seed and n_boot.
  • Always state what the bars mean and prefer showing raw data when samples are small.

Next: Time Series and Rolling Averages