Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Regression Plots (lmplot, regplot)

Add trend lines and confidence intervals using regplot and lmplot.

Why regression plots?

Regression plots help you see:

  • Trend direction
  • Strength of relationship (visual)
  • Outliers that affect the trend
diagram What regplot draws mermaid
regplot combines a scatter plot with a fitted line and a shaded confidence band around it.

regplot (axes-level)

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.regplot(data=tips, x="total_bill", y="tip")
plt.title("Tip vs total bill")
plt.tight_layout()
plt.show()

lmplot (figure-level, great for facets)

Python
import seaborn as sns
 
tips = sns.load_dataset("tips")
 
sns.lmplot(data=tips, x="total_bill", y="tip", hue="sex")

Visualize it

The line summarizes the overall trend; the shaded band shows how confident the fit is — narrower where data is dense, wider near the edges:

sketch Scatter plot with a fitted trend line p5.js
Dots are individual data points; the blue line is the fitted trend, with a soft confidence band around it.

Non-linear patterns

You can try polynomial fits.

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.regplot(data=tips, x="total_bill", y="tip", order=2)
plt.title("Quadratic trend")
plt.tight_layout()
plt.show()

Tips

  • Regression plots show association, not causation.
  • Outliers can heavily affect the fitted line.
  • Use transformations (log) if relationships are multiplicative.

Check your understanding

quizCheck yourself
  1. What does sns.regplot draw?

    Show answer

    A — A scatter plot with a fitted linear regression line — regplot makes a scatter plot and fits a linear regression line with a confidence band.

  2. What is the key difference between regplot and lmplot?

    Show answer

    B — lmplot is figure-level and supports hue and facets — regplot is axes-level while lmplot is figure-level and great for facets.

  3. What does order=2 do in sns.regplot(..., order=2)?

    Show answer

    B — Fits a quadratic (polynomial) trend — order sets the polynomial degree, so order=2 is a quadratic trend for non-linear patterns.

  4. The tip versus total_bill line slopes upward. What can you conclude from the plot alone?

    Show answer

    B — An association, but not causation — The lesson stresses that regression plots show association, not causation, and outliers can heavily affect the line.

Next

Continue to Joint Plots to see the same scatter relationship alongside each variable's own distribution.

🧪 Try It Yourself

Exercise 1 – Fit a Regression Line

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Facet the Regression by Group

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Try a Quadratic Fit

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did