Regression Plots (lmplot, regplot)
Why regression plots?
Section titled “Why regression plots?”Regression plots help you see:
- Trend direction
- Strength of relationship (visual)
- Outliers that affect the trend
flowchart LR A["x, y columns"] --> B["Scatter every point"] A --> C["Fit a regression line
(linear by default)"] C --> D["Shade a confidence
band around the line"] B --> E["regplot figure"] D --> E
regplot (axes-level)
Section titled “regplot (axes-level)”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)
Section titled “lmplot (figure-level, great for facets)”import seaborn as sns
tips = sns.load_dataset("tips")
sns.lmplot(data=tips, x="total_bill", y="tip", hue="sex")Visualize it
Section titled “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:
Non-linear patterns
Section titled “Non-linear patterns”You can try polynomial fits.
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()- Regression plots show association, not causation.
- Outliers can heavily affect the fitted line.
- Use transformations (log) if relationships are multiplicative.
Continue to Joint Plots to see the same scatter relationship alongside each variable’s own distribution.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Fit a Regression Line
Section titled “Exercise 1 – Fit a Regression Line”Exercise 2 – Facet the Regression by Group
Section titled “Exercise 2 – Facet the Regression by Group”Exercise 3 – Try a Quadratic Fit
Section titled “Exercise 3 – Try a Quadratic Fit”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading