Visualizing Regression Plots

Learn how to harness Plotly figures to assist in visualizing and analysing regression models in Python.

Scatter plot smoothing

Scatter plot smoothing is a statistical technique used to gain an understanding of the general patterns in the data by creating a curve with a smoothed estimate of the relationship between two variables, x and y. It is also particularly useful for assessing bivariate outliers and influential observations that may affect the relationship between the variables.


This is a macroeconomic datasetJ. W. Longley (1967) An appraisal of least-squares programs from the point of view of the user. Journal of the American Statistical Association 62, 819–841. recording economic variables of:

  • GNP.deflator: GNP implicit price deflator (1954=100)

  • GNP: Gross National Product

  • Unemployed: Number of unemployed

  • Armed.Forces: Number of people in the armed forces

  • Population: ‘noninstitutionalized’ population ≥ 14 years of age

  • Year: The year (time)

  • Employed: Number of people employed

Note: This dataset is often used to demonstrate how regressing Employed on the remaining variables causes multicollinearity.

Press + to interact
# Import libraries
import pandas as pd
import numpy as np
# Import datasets
longley = pd.read_csv('/usr/local/csvfiles/longley.csv')
print(longley.head())

Linear regression

Plotly Express allows us to fit a linear regression to a dataset with an x and y variable. We do this by placing a trendline="ols" argument to specify that we wish to use an ordinary least squares regression model. We can set the color of the line using trendline_color_override. By hovering over the trend line, we gain an insight into the slope, intercept, coefficient of determination (R2R^2), and prediction y^\hat{y} ...