Skip to content

Plotting

Requires the plot extra: pip install "scikit-opls[plot]".

Diagnostic plots for OPLS models.

The public surface follows scikit-learn's Display convention: each plot is a class with a :meth:from_estimator constructor that computes the plotted arrays, a :meth:plot method that draws them, and stored ax_ / figure_ handles.

matplotlib is an optional dependency (pip install scikit-opls[plot]) and is imported lazily inside :meth:plot, so importing this module never requires it.

OPLSScoresDisplay

OPLSScoresDisplay(
    *,
    t_predictive: NDArray[float64],
    t_orthogonal: NDArray[float64],
    y: NDArray | None = None,
    predictive_component: int = 0,
    orthogonal_component: int = 0,
    has_orthogonal: bool = True,
)

Predictive vs orthogonal score scatter for an OPLS-family model.

Works for :class:~scikit_opls.OPLS, :class:~scikit_opls.OPLSDA, a pipeline ending in one, or a fitted search meta-estimator exposing best_estimator_ around either shape. Construct with :meth:from_estimator.

Parameters:

Name Type Description Default
t_predictive ndarray of shape (n_samples,)

Selected predictive score per sample.

required
t_orthogonal ndarray of shape (n_samples,)

Selected orthogonal score per sample (zeros when the model has none).

required
y ndarray of shape (n_samples,) or None

Optional labels used to colour the points.

None

Attributes:

Name Type Description
ax_ matplotlib Axes

The axes drawn on (set by :meth:plot).

figure_ matplotlib Figure

The parent figure (set by :meth:plot).

scatter_ matplotlib PathCollection or list of PathCollection

The scatter plot artist(s) (set by :meth:plot).

has_orthogonal bool

Whether the fitted model has any orthogonal component. When False the orthogonal axis is a constant-zero placeholder and labelled as such.

from_estimator classmethod

from_estimator(
    estimator: BaseEstimator,
    X: ArrayLike,
    y: ArrayLike | None = None,
    *,
    predictive_component: int = 0,
    orthogonal_component: int = 0,
    ax: Axes | None = None,
) -> OPLSScoresDisplay

Compute the scores from a fitted estimator and plot them.

Parameters:

Name Type Description Default
estimator OPLS, OPLSDA, Pipeline or search meta-estimator

A fitted estimator. When passing a pipeline, pass raw X as expected by that pipeline. When passing the final OPLS-family step directly, pass the already transformed matrix seen by that step.

required
X array-like of shape (n_samples, n_features)

Samples to project.

required
y array-like of shape (n_samples,)

Optional labels used to colour the points.

None
predictive_component int

The index of the predictive PLS component to plot.

0
orthogonal_component int

The index of the orthogonal component to plot.

0
ax matplotlib Axes

Target axes; a new figure/axes is created when None.

None

Returns:

Name Type Description
display OPLSScoresDisplay

The plotted display, with ax_ / figure_ set.

plot

plot(ax: Axes | None = None) -> OPLSScoresDisplay

Draw the score scatter on ax (or a fresh axes).

Parameters:

Name Type Description Default
ax matplotlib Axes

Target axes; a new figure/axes is created when None.

None

Returns:

Name Type Description
display OPLSScoresDisplay

self, with ax_ / figure_ populated.

SPlotDisplay

SPlotDisplay(
    *,
    covariance: NDArray[float64],
    correlation: NDArray[float64],
    component: int = 0,
)

S-plot: covariance vs correlation of each feature with the predictive score.

.. note:: SPlotDisplay is intended for the fitted training data. For x_space="centered", X is centered by the fitted model mean. For x_space="scaled", X is centered/scaled by the fitted model preprocessing. For x_space="subset-centered", X is centered by the provided subset mean.

When ``estimator`` is a pipeline, the S-plot is computed in the feature
space received by the final OPLS-family step after upstream transformations.
Points may therefore represent transformed features rather than original
input columns.

Accepts :class:~scikit_opls.OPLS, :class:~scikit_opls.OPLSDA, a pipeline ending in one, or a fitted search meta-estimator exposing best_estimator_ around either shape. Construct with :meth:from_estimator.

Parameters:

Name Type Description Default
covariance ndarray of shape (n_features,)

Covariance of each feature with the selected predictive score.

required
correlation ndarray of shape (n_features,)

Correlation of each feature with the selected predictive score.

required

Attributes:

Name Type Description
ax_ matplotlib Axes

The axes drawn on (set by :meth:plot).

figure_ matplotlib Figure

The parent figure (set by :meth:plot).

scatter_ matplotlib PathCollection

The scatter plot artist (set by :meth:plot).

from_estimator classmethod

from_estimator(
    estimator: BaseEstimator,
    X: ArrayLike,
    *,
    component: int = 0,
    x_space: str = "centered",
    ax: Axes | None = None,
) -> SPlotDisplay

Compute the S-plot arrays from a fitted estimator and plot them.

Parameters:

Name Type Description Default
estimator OPLS, OPLSDA, Pipeline or search meta-estimator

A fitted estimator. When passing a pipeline, pass raw X as expected by that pipeline. When passing the final OPLS-family step directly, pass the already transformed matrix seen by that step.

required
X array-like of shape (n_samples, n_features)

Samples to project.

required
component int

The index of the predictive PLS component to plot.

0
x_space ('centered', 'scaled', 'subset-centered')

Feature space used for the covariance/correlation axes. "centered" uses original feature units centered by the fitted training mean, "scaled" uses the model-scaled feature space, and "subset-centered" centers the provided X subset by its own mean.

"centered"
ax matplotlib Axes

Target axes; a new figure/axes is created when None.

None

Returns:

Name Type Description
display SPlotDisplay

The plotted display, with ax_ / figure_ set.

plot

plot(ax: Axes | None = None) -> SPlotDisplay

Draw the S-plot scatter on ax (or a fresh axes).

Parameters:

Name Type Description Default
ax matplotlib Axes

Target axes; a new figure/axes is created when None.

None

Returns:

Name Type Description
display SPlotDisplay

self, with ax_ / figure_ populated.

options: members:

  • OPLSScoresDisplay
  • SPlotDisplay