Regression Plots
A regression plot combines a scatter plot with a fitted regression line, giving you both the raw relationship between two variables and a statistical summary of that relationship in one view.
Why Use a Regression Plot
- Visually confirms whether a linear (or polynomial) fit is appropriate for the data
- Highlights the direction and approximate strength of the relationship
- Makes it easy to spot points that deviate strongly from the fitted trend
Plotting with Seaborn
import seaborn as sns
import matplotlib.pyplot as plt
sns.regplot(x="hours_studied", y="exam_score", data=df, color="#ff5421")
plt.title("Hours Studied vs Exam Score")
plt.show()
Confidence Interval Band
Seaborn's regplot() shades a confidence interval around the fitted line by default, giving a visual sense of the uncertainty in the estimate - a narrower band indicates a more confident fit.
Regression Plots vs Regression Models
A regression plot is a diagnostic and communication tool, not a substitute for fitting an actual regression model (as covered in Simple and Multiple Linear Regression). Use it before and after model building to sanity-check assumptions and results.
Reading What the Plot Is Telling You
Once the line is drawn, look at three things before trusting it. First, the slope direction: upward means the variables rise together, downward means one falls as the other rises. Second, the spread of points around the line: tightly packed points mean the line explains a lot of the variation, while a wide cloud means it explains little even if the slope looks clear. Third, the shape of the spread: if points fan out as x grows, the variability is not constant, which breaks an assumption of ordinary linear regression.
Useful regplot and lmplot Options
Seaborn's regplot() can do more than a straight line. A few arguments cover most real needs:
import seaborn as sns
import matplotlib.pyplot as plt
# curved relationship: fit a second-degree polynomial
sns.regplot(x="engine_size", y="mileage", data=cars, order=2)
# many overlapping points: make them transparent, hide the CI band
sns.regplot(x="age", y="income", data=df, ci=None,
scatter_kws={"alpha": 0.3, "s": 10},
line_kws={"color": "black"})
# binary outcome (0/1): fit a logistic curve (requires statsmodels)
sns.regplot(x="hours_studied", y="passed", data=df, logistic=True)
# separate fitted lines per group, one panel per region
sns.lmplot(x="ad_spend", y="sales", data=shops, hue="channel", col="region")
plt.show()
The key difference between the two functions is that regplot() draws on a single set of axes you control, while lmplot() builds a whole figure and can split the data by categories using hue, col and row.
Checking the Fit with a Residual Plot
A residual is the gap between an actual value and the value the line predicts. Plotting residuals against x makes problems much easier to see than on the original chart.
sns.residplot(x="hours_studied", y="exam_score", data=df)
plt.axhline(0, linestyle="--")
plt.show()
A healthy residual plot looks like random noise scattered evenly around zero. A U-shape or arch means the relationship is curved and a straight line is the wrong model. A funnel shape means the error grows with x. A handful of points far from zero are worth investigating individually, because they may be data entry errors or genuinely unusual cases.
Mistakes That Lead to Wrong Conclusions
- Reading the line as cause and effect. A clear trend shows association only; a third variable may drive both.
- Extending the line beyond the data. The fit says nothing reliable about x values far outside the observed range.
- Ignoring a single extreme point. One outlier at the edge of the range can tilt the whole line. Try the plot with and without it.
- Hiding groups. Two groups with opposite trends can combine into a misleading single line. Use
hueto check.
Quick routine: scatter first, add the line, check the residual plot, then split by any important category before presenting the result.