Overview
Concrete tool for visualization rendering provided by the Yellowbrick library.
Description
The Visualizer class in yellowbrick.base is the root of Yellowbrick's visual object hierarchy. It inherits from scikit-learn's BaseEstimator and defines the complete lifecycle for creating, storing, and rendering visual artifacts using matplotlib. Every visualizer in the Yellowbrick library -- from feature analysis plots to model evaluation diagnostics -- inherits from this class and follows its rendering contract.
The class defines five key methods that form the rendering pipeline:
- __init__(ax, fig, **kwargs): Stores references to optional matplotlib Axes and Figure objects, and extracts optional size, color, and title keyword arguments for styling.
- fit(X, y, **kwargs): The primary entry point for learning from data. The base implementation simply returns self; subclasses override this to perform actual data analysis and typically call draw() internally.
- draw(**kwargs): Produces the core graphical elements on the matplotlib Axes. This is an abstract method (raises NotImplementedError) that subclasses must implement. It is called during fit() or score(), not directly by the user.
- finalize(**kwargs): Applies subclass-specific axes finalization such as setting titles, axis labels, legends, and tick formatting. The base implementation returns self.ax. Called automatically by show().
- show(outpath, clear_figure, **kwargs): Orchestrates the complete rendering: calls finalize(), then either saves the figure to outpath via plt.savefig() or displays it interactively via plt.show(). Optionally clears the figure afterward. Returns the Axes object to support inline display in Jupyter notebooks.
Usage
Import and use the Visualizer class when building a custom Yellowbrick visualizer. End users typically interact with concrete subclasses (e.g., PCADecomposition, ConfusionMatrix) and call show() as the final step after fit() and optionally score(). The show() method is the standard way to render any Yellowbrick visualization, whether to screen or to a file.
Code Reference
Source Location
- Repository: yellowbrick
- File: yellowbrick/base.py
- Lines: 35-279 (Visualizer class); key methods at L77-83 (__init__), L141-167 (fit), L173-187 (draw), L189-204 (finalize), L206-252 (show)
Signature
class Visualizer(BaseEstimator):
def __init__(self, ax=None, fig=None, **kwargs):
...
def fit(self, X, y=None, **kwargs):
...
def draw(self, **kwargs):
...
def finalize(self, **kwargs):
...
def show(self, outpath=None, clear_figure=False, **kwargs):
...
Import
from yellowbrick.base import Visualizer
I/O Contract
Inputs
__init__
| Name |
Type |
Required |
Description
|
| ax |
matplotlib.axes.Axes |
No |
The axis to plot the figure on. If None, the current axes will be used or generated via plt.gca().
|
| fig |
matplotlib.figure.Figure |
No |
The figure to plot on. If None, the current figure will be used or generated via plt.gcf().
|
| size |
tuple of (width, height) |
No |
Specify a size in pixels for the figure. Passed via kwargs.
|
| color |
str or colormap |
No |
Specify a color, colormap, or palette for the figure. Passed via kwargs.
|
| title |
str |
No |
Specify the title of the figure. Passed via kwargs.
|
fit
| Name |
Type |
Required |
Description
|
| X |
ndarray or DataFrame of shape (n, m) |
Yes |
A matrix of n instances with m features.
|
| y |
ndarray or Series of length n |
No |
An array or series of target or class values.
|
| kwargs |
dict |
No |
Keyword arguments passed to the drawing functionality or to the scikit-learn API.
|
show
| Name |
Type |
Required |
Description
|
| outpath |
str |
No |
File path to save the figure to disk. If None, the figure is displayed interactively.
|
| clear_figure |
bool |
No |
When True, clears the figure after saving or showing. Useful for consecutive plots. Default: False.
|
| kwargs |
dict |
No |
Additional keyword arguments passed to plt.savefig() when outpath is provided.
|
Outputs
fit
| Name |
Type |
Description
|
| self |
Visualizer |
Returns self to support scikit-learn pipeline chaining.
|
show
| Name |
Type |
Description
|
| ax |
matplotlib.axes.Axes |
Returns the matplotlib Axes object to ensure display in Jupyter notebooks.
|
Usage Examples
Basic Usage
from yellowbrick.features import PCADecomposition
from sklearn.datasets import load_iris
# Load data
X, y = load_iris(return_X_y=True)
# Instantiate visualizer (inherits from Visualizer)
viz = PCADecomposition()
# Fit the visualizer to data (calls draw internally)
viz.fit(X, y)
# Render the visualization to screen
viz.show()
Saving to File
from yellowbrick.features import PCADecomposition
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
viz = PCADecomposition()
viz.fit(X, y)
# Save to disk instead of displaying interactively
viz.show(outpath="pca_plot.png")
Custom Axes and Figure
import matplotlib.pyplot as plt
from yellowbrick.features import PCADecomposition
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
# Create custom figure and axes
fig, ax = plt.subplots(figsize=(10, 6))
# Pass custom axes to the visualizer
viz = PCADecomposition(ax=ax)
viz.fit(X, y)
viz.show()
Related Pages
Implements Principle
Requires Environment
Uses Heuristic