Fit on a declared population, show the data behind the line, and keep confidence and prediction bands distinct.
Regression overlays: separate fitted trend from individual spread
Define the fit population
A line on a scatterplot is a model output, not a property of the pixels. State which rows entered the fit, the x and y units, the time window and exclusions. If the visible chart filters to one region while the line is still fitted across all regions, label that difference or refit intentionally. A pooled line can hide opposing group trends. Plot groups separately before deciding whether a shared slope is defensible. Overplotting and sampling can make a line look better supported than the visible points imply.
Do not mix two kinds of band
A confidence band for the conditional mean describes uncertainty in the estimated average curve under the model. A prediction band for a new observation includes individual variation and is normally wider. Label the band type, level and fitting method; never call an arbitrary shaded region a confidence interval. Interval endpoints should be retained in the exported data. A narrow mean band does not promise that the next delivery will land near the line.
Check influential points and model shape
One distant x value may pull a straight line through a cloud that otherwise has little trend. Inspect residuals and report the fit's valid x range. If the relationship bends, a straight line can be a poor summary even when it is easy to draw. Do not extend the overlay far beyond observed x values unless extrapolation is the explicit task and its uncertainty is shown. Q–Q diagnostics can reveal distribution departures in residuals but do not repair confounding or nonlinearity.
Keep explanation separate from causation
A rising line means the plotted variables co-vary in the selected records. It does not establish that changing x will change y. Annotate outages, policy changes or cohorts where they matter, but do not turn temporal coincidence into a mechanism. Event annotations have the same limit. Publish the coefficients and row count alongside the plot, and offer a table of fitted values if the chart is interactive. A model revision should change its version in the export so the old and new lines can be reproduced.
Worked contract
from math import isfinite
def fit_straight_line(points: list[tuple[float, float]]) -> tuple[float, float]:
if len(points) < 3 or any(not (isfinite(x) and isfinite(y))
for x, y in points):
raise ValueError("at least three finite pairs required")
mean_x = sum(x for x, _ in points) / len(points)
mean_y = sum(y for _, y in points) / len(points)
xx = sum((x - mean_x) ** 2 for x, _ in points)
if xx == 0:
raise ValueError("x values need variation")
slope = sum((x - mean_x) * (y - mean_y) for x, y in points) / xx
return mean_y - slope * mean_x, slope
dispatch_records = [(2, 47), (3, 59), (4, 71), (5, 83)]
intercept, slope = fit_straight_line(dispatch_records)
assert (intercept, slope) == (23.0, 12.0)
assert intercept + slope * 6 == 95.0Performance and operating cost
Computing a simple least-squares line takes O(N) time and O(1) extra space for N pairs. The code deliberately does not calculate uncertainty bands: those require a named model, assumptions, residual variance and an interval method. It is only a descriptive fit; the four exact points are a code check, not evidence of predictive accuracy. Keep the raw rows or an auditable snapshot when shipping a chart with a fitted overlay.
Common Mistakes
- Fitting on all rows while displaying only a filtered subset without saying so.
- Calling a mean-confidence band a prediction interval for the next record.
- Extrapolating a straight line beyond observed x values as though support were unchanged.
- Interpreting an association line as proof of a causal effect.
Validation drill
Fit dispatch delay against queue depth for 188 records. Compare pooled and depot-specific slopes, identify the most influential record, and label any uncertainty band with its exact meaning.
Read next
- Dense scatterplots: reveal overlap without hiding rare observations
- Asymmetric error bars: tell readers what each endpoint means
- Q–Q plots: compare distribution shape beyond one summary number
- Chart annotations: align events to the right clock without claiming causality
