Implementation:DistrictDataLabs Yellowbrick Bestfit Drawing Utilities
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Regression |
| Last Updated | 2026-02-08 05:00 GMT |
Overview
Utility functions for drawing best fit regression lines and identity lines on matplotlib axes, provided by the Yellowbrick library.
Description
The bestfit module provides convenience functions for fitting regression models (linear, quadratic) to data and rendering the resulting curves onto matplotlib axes. It leverages scikit-learn's LinearRegression and PolynomialFeatures pipeline for model fitting and includes MSE-based model selection. The module also provides a dynamic identity line (y=x) useful for prediction error plots.
Usage
Import these utilities when building custom visualizations that require overlay of regression trend lines or identity reference lines. The draw_best_fit function is used internally by Yellowbrick visualizers such as ResidualsPlot and PredictionError, while draw_identity_line is used in prediction error plots.
Code Reference
Source Location
- Repository: DistrictDataLabs_Yellowbrick
- File: yellowbrick/bestfit.py
- Lines: 1-302
Signature
def draw_best_fit(X, y, ax, estimator="linear", **kwargs):
"""Attempts to draw the best fit line for the data."""
def fit_select_best(X, y):
"""Selects the best fit between linear and quadratic via MSE comparison."""
def fit_linear(X, y):
"""Uses OLS to fit a regression."""
def fit_quadratic(X, y):
"""Uses OLS with Polynomial order 2."""
def draw_identity_line(ax=None, dynamic=True, **kwargs):
"""Draws a 45-degree identity line (y=x) on the axes."""
Import
from yellowbrick.bestfit import draw_best_fit, draw_identity_line
I/O Contract
Inputs (draw_best_fit)
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like | Yes | Feature data for fitting |
| y | array-like | Yes | Target data for fitting |
| ax | matplotlib.Axes | Yes | Axes to draw on |
| estimator | str | No | Fit type: "linear", "quadratic", or "select_best" (default: "linear") |
Outputs
| Name | Type | Description |
|---|---|---|
| ax | matplotlib.Axes | The axes with best fit line drawn |
Usage Examples
import numpy as np
import matplotlib.pyplot as plt
from yellowbrick.bestfit import draw_best_fit, draw_identity_line
# Draw a best fit line on scatter data
fig, ax = plt.subplots()
X = np.random.rand(100, 1)
y = 2 * X.ravel() + np.random.randn(100) * 0.5
ax.scatter(X, y, alpha=0.5)
draw_best_fit(X, y, ax, estimator="select_best")
plt.show()
# Draw an identity line for prediction error visualization
fig, ax = plt.subplots()
draw_identity_line(ax, dynamic=True, color="gray", linestyle="--")
plt.show()