ExplainedVariancePlot#
- class chemotools.plotting.ExplainedVariancePlot(explained_variance_ratio: ndarray, *, threshold: float | None = 0.95)[source]
Bases:
BasePlotVisualize explained variance by component with cumulative variance.
Shows both individual and cumulative explained variance ratios across components. Works with any decomposition method (PCA, PLS, ICA, etc.) to help determine the optimal number of components.
Works with: - PCA: Use pca.explained_variance_ratio_ directly - PLS: Use chemotools.models.PLSRegression for automatic variance calculation - Any method: Just provide an array of variance ratios per component
- Parameters:
explained_variance_ratio (np.ndarray) – Array of explained variance ratios for each component. Should be 1D array with values between 0 and 1. For PCA, use model.explained_variance_ratio_ directly. For PLS, use chemotools.models.PLSRegression which provides explained_x_variance_ratio_ and explained_y_variance_ratio_.
threshold (float or None, optional) – If provided, draws a horizontal dashed line at this variance level. Common values are 0.90, 0.95, 0.99. Default is 0.95.
- Variables:
cumulative_variance (np.ndarray) – Cumulative sum of explained variance ratios.
Examples
Example 1: PCA (simplest case)
>>> from sklearn.decomposition import PCA >>> pca = PCA(n_components=10) >>> pca.fit(X) >>> plot = ExplainedVariancePlot(pca.explained_variance_ratio_) >>> fig = plot.show(title="PCA Explained Variance")
Example 2: PLS - Now just as simple!
>>> from chemotools.models import PLSRegression >>> from chemotools.plotting import ExplainedVariancePlot >>> >>> pls = PLSRegression(n_components=5) >>> pls.fit(X, y) >>> >>> # Variance ratios automatically available! >>> plot_x = ExplainedVariancePlot(pls.explained_x_variance_ratio_) >>> fig = plot_x.show(title="PLS Explained Variance in X-space") >>> >>> plot_y = ExplainedVariancePlot(pls.explained_y_variance_ratio_) >>> fig = plot_y.show(title="PLS Explained Variance in Y-space")
Example 3: Side-by-side PLS comparison
>>> import matplotlib.pyplot as plt >>> fig, axes = plt.subplots(1, 2, figsize=(14, 5)) >>> >>> ExplainedVariancePlot(pls.explained_x_variance_ratio_).render(ax=axes[0]) >>> axes[0].set_title('X-space (Predictors)') >>> >>> ExplainedVariancePlot(pls.explained_y_variance_ratio_).render(ax=axes[1]) >>> axes[1].set_title('Y-space (Response)')
Example 4: Custom threshold and labels
>>> plot = ExplainedVariancePlot(pca.explained_variance_ratio_, threshold=0.90) >>> fig = plot.show(xlabel="PC Number", ylabel="Variance Explained")
- show(*, figsize: Tuple[float, float] | None = None, title: str | None = None, xlabel: str | None = None, ylabel: str | None = None, xlim: Tuple[float, float] | None = None, ylim: Tuple[float, float] | None = None, **kwargs: Any) Figure[source]
Create and return a complete figure with the explained variance plot.
This method handles figure creation and then delegates to render().
- Parameters:
figsize (tuple[float, float], optional) – Figure size in inches (width, height).
title (str, optional) – Figure title.
xlabel (str, optional) – Custom x-axis label. If None, uses existing label or default.
ylabel (str, optional) – Custom y-axis label. If None, uses existing label or default.
xlim (tuple[float, float], optional) – X-axis limits as (xmin, xmax).
ylim (tuple[float, float], optional) – Y-axis limits as (ymin, ymax).
**kwargs (Any) – Additional keyword arguments passed to the render() method.
- Returns:
The matplotlib Figure object containing the plot.
- Return type:
Figure
- render(ax: Axes | None = None, *, xlabel: str | None = None, ylabel: str | None = None, xlim: tuple[float, float] | None = None, ylim: tuple[float, float] | None = None, **kwargs: Any) tuple[Figure, Axes][source]
Render the plot on the given axes or create new ones.
- Parameters:
ax (Axes, optional) – Matplotlib axes to render on. If None, current axes are used.
xlabel (str, optional) – Label for x-axis. Default is “Component”.
ylabel (str, optional) – Label for y-axis. Default is “Explained Variance Ratio”.
xlim (tuple[float, float], optional) – X-axis limits (min, max).
ylim (tuple[float, float], optional) – Y-axis limits (min, max).
**kwargs (Any) – Additional keyword arguments for plot customization.
- Returns:
The matplotlib Axes object.
- Return type:
Axes