Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 38 ยท Regression and Matrices

Regression Plots with regplot and lmplot

Draw scatter plots with a fitted regression line and confidence band using regplot and lmplot, add one line per group, and fit polynomial and logistic models.

A scatter plot shows the points. A regression plot adds the trend: a fitted line and a shaded band that says how sure the fit is. Seaborn gives you two functions for this, regplot() and lmplot(). They fit the same models, but they are built differently, and knowing the difference saves you from many layout surprises.

In this lesson you draw a line through a cloud of points, add one line per group, bend the line with a polynomial, and fit a logistic curve to a yes/no outcome. All the data is synthetic and seeded, so your pictures match ours.

Seaborn is installed from PyPI with micropip in the runner (the first run takes a few seconds). On your own machine run pip install seaborn and drop the install lines.

regplot: one scatter plot, one fit

regplot() is an axes-level function. It draws on one Matplotlib Axes, so it plays well with plt.subplots(). Pass x, y and data:

Python Scatter plot with a regression line

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 line is an ordinary least squares fit. The shaded band is a 95% confidence interval for that line, computed by bootstrapping: seaborn refits the model on 1,000 resampled copies of your data and plots the spread. It is a statement about where the line might lie, not about where individual points fall. More on this in error bars and confidence intervals.

Useful regplot() arguments:

Argument Effect
ci=None Hide the confidence band (faster, cleaner).
ci=99 Use a 99% band instead of 95%.
order=2 Fit a polynomial of that degree.
logx=True Fit y against log(x).
lowess=True Fit a local smoother (needs statsmodels).
robust=True Reduce the pull of outliers (needs statsmodels).
x_jitter=0.1 Add noise to discrete x values so points do not stack.
scatter_kws, line_kws Dictionaries passed to the scatter and line artists.
fit_reg=False Draw only the points.

Styling the points and the line

The two artists are styled separately. scatter_kws goes to Matplotlib's scatter, line_kws to plot:

Python scatter_kws and line_kws

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.

lmplot: a figure-level fit with hue and facets

lmplot() is figure-level. It builds a FacetGrid, so it can split the data across columns and rows and colour by a category, with one fit per subset. Because it makes its own figure, you size it with height and aspect rather than figsize, and you do not pass ax.

Python One regression line per group

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.

Two lines on one axes comes from hue. Replace it with col="meal" and you get two side-by-side panels with the same scale, which is easier to read when the groups overlap heavily.

Use regplot() when you need the fit inside a layout you control (a dashboard of subplots). Use lmplot() when the question is "how does the relationship differ by group", because hue, col and row are built in.

Curves: polynomial, log and lowess fits

A straight line is a modelling choice, not a fact. When the data bends, raise the order:

Python Linear versus quadratic fit

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.

lowess=True needs no assumed shape: it fits many small local lines and joins them. It has no confidence band, and it is slow on thousands of points, so treat it as an exploration tool.

Logistic regression for yes/no outcomes

When y is 0 or 1, a straight line leaves the 0-1 range. Pass logistic=True and seaborn fits a logistic curve (needs statsmodels). Jitter the points vertically so they do not all sit on two lines:

Python Logistic fit

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.

Common mistakes

  • Passing figsize to lmplot(). It is not accepted; use height and aspect.
  • Reading the band as a prediction interval. It describes the line, not new observations.
  • Fitting across groups that differ. One line through two clusters can hide opposite trends inside each. This is Simpson's paradox; use hue to check.
  • Extrapolating. The line is drawn only over the data range (truncate=True is the default), which is a hint to stay inside it.

Next you learn how to check whether the fit is any good with residuals. For the underlying scatter plot, see Matplotlib scatter plots.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function draws on an Axes you pass in with ax=?

    regplot() is axes-level and accepts ax=. lmplot() is figure-level and builds its own grid.
  2. 2. What does the shaded band around the regression line show?

    Seaborn refits the model on resampled data and shades the spread of those lines. It says nothing about where single points fall.
  3. 3. How do you fit a quadratic curve with regplot?

    order sets the polynomial degree.
  4. 4. You want one fit per category, each in its own panel. What do you use?

    lmplot is built on FacetGrid and supports hue, col and row.

Summary

  • regplot() is axes-level, lmplot() is figure-level with hue, col, row, height and aspect.
  • The band is a bootstrapped 95% confidence interval for the line; ci=None hides it.
  • order, logx, lowess, robust and logistic change the model; the last three need statsmodels.
  • Style points and line with scatter_kws and line_kws.

Next: Residual Plots and Model Fit