Skip to content

How to plot a regression line with a stated uncertainty interval in Seaborn (with Code)

The Short Answer (TL;DR)

Set ci=95 in sns.regplot to draw a 95% bootstrap confidence band around the fitted regression estimate. Label the band as uncertainty in the fitted mean response, because it is not a prediction interval for a new observation.

import numpy as np, seaborn as sns
rng = np.random.default_rng(19); x = np.linspace(0, 10, 24); y = 1.5 + 0.8 * x + rng.normal(size=24)
sns.regplot(x=x, y=y, ci=95, n_boot=1000, seed=19).set_title("95% bootstrap CI for the fitted mean")

Detailed Implementation

This example uses synthetic data and does not need a network connection. Install the recorded package versions in a separate Python environment before running it.

python -m pip install numpy==2.5.4 pandas==3.0.6 matplotlib==3.11.2 seaborn==0.13.2
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

rng = np.random.default_rng(19)
x = np.linspace(0, 10, 24)
y = 1.5 + 0.8 * x + rng.normal(size=x.size)
fig, ax = plt.subplots(figsize=(6.0, 3.8), layout="constrained")
sns.regplot(x=x, y=y, ci=95, n_boot=1000, seed=19,
             scatter_kws={"alpha": 0.75}, line_kws={"color": "#176b87"}, ax=ax)
ax.set(xlabel="Synthetic input", ylabel="Synthetic response",
       title="Linear fit with 95% bootstrap CI for the mean response")
fig.savefig("regression-bootstrap-confidence.png", dpi=160)
plt.show()

The chart contains 24 observations, one fitted straight line and a shaded 95% bootstrap confidence band. The line agrees numerically with the ordinary least-squares fit to these synthetic observations.

What does the shaded band describe?

The band estimates uncertainty in the regression curve at the plotted x values using resampled observations. It does not describe the spread of individual responses. A new observation has its own residual variation, which requires a prediction interval.

Why set both n_boot and seed?

n_boot sets the number of bootstrap resamples, and seed makes those resamples reproducible. The separate random generator creates the synthetic observations. More resamples can reduce Monte Carlo variability, but they do not repair a misspecified model or an unsuitable sample.

Which assumptions need checking?

Interpret the default straight line as an approximation to the conditional mean. Check outliers, nonlinear patterns and the sampling design before using the band for inference. Ordinary row resampling assumes suitably independent observations; clustered or serial data need an appropriate resampling or modelling approach.

Mathematical Context

Ordinary least squares chooses an intercept and slope to minimize the sum of squared residuals. The fitted mean at x is the intercept plus the slope times x. Seaborn repeatedly resamples observations and refits the curve to estimate a pointwise confidence band. The nominal 95% level is an inferential convention, not a claim that 95% of the observed points lie inside the band or that the entire curve has simultaneous 95% coverage.

\[ \hat y(x)=\hat\beta_0+\hat\beta_1 x,\qquad \min_{\beta_0,\beta_1}\sum_i(y_i-\beta_0-\beta_1x_i)^2 \]

Reproducibility

Executed with Python 3.12.14; numpy 2.5.4, pandas 3.0.6, matplotlib 3.11.2, seaborn 0.13.2. Automated checks confirm the stated output properties.

Primary documentation

Related guides