LoadingsPlot#

class chemotools.plotting.LoadingsPlot(loadings: ndarray, *, feature_names: ndarray | list | None = None, components: int | list[int] = 0, component_label: str = 'PC')[source]

Bases: BasePlot

Loadings plot implementing Display protocol for model inspection.

This class creates line plots of model loadings (feature weights), following the same design pattern as SpectraPlot and ScoresPlot. Supports plotting single or multiple components overlaid on the same plot.

Parameters:
  • loadings (np.ndarray) – Loadings array with shape (n_features, n_components).

  • feature_names (np.ndarray or list, optional) – Names/values for features (x-axis). Can be wavelengths, wavenumbers, feature indices, etc. If None, uses feature indices [0, 1, 2, …].

  • components (int or list[int], optional) – Which component(s) to plot. Can be: - Single int (default 0): plots one component - List of ints: plots multiple components overlaid with legend Uses 0-based indexing.

  • component_label (str, optional) – Prefix for component naming in legend and titles (default “PC”). Use “LV” for PLS models, “PC” for PCA models, “IC” for ICA, etc.

Raises:

ValueError – If component index exceeds the available components in the loadings array.

Examples

Basic usage with single component:

>>> loadings = model.components_.T  # Shape: (n_features, n_components)
>>> wavenumbers = np.linspace(400, 2500, n_features)
>>> plot = LoadingsPlot(loadings, feature_names=wavenumbers, components=0)
>>> fig = plot.show(title="PC1 Loadings")

Plot multiple components overlaid:

>>> plot = LoadingsPlot(
...     loadings,
...     feature_names=wavenumbers,
...     components=[0, 1, 2],  # Plot PC1, PC2, PC3 together
... )
>>> fig = plot.show(
...     title="First 3 Principal Components",
...     xlabel='Wavenumber (cm⁻¹)',
...     ylabel='Loading Coefficient'
... )

With custom axis labels:

>>> plot = LoadingsPlot(
...     loadings,
...     feature_names=wavenumbers,
...     components=0
... )
>>> fig = plot.show(
...     title="PLS LV1 Loadings",
...     xlabel="Wavenumber (cm⁻¹)",
...     ylabel="Loading Coefficient"
... )

Zoom into a spectral region:

>>> plot = LoadingsPlot(loadings, feature_names=wavenumbers, components=[0, 1])
>>> fig = plot.show(title="C-H Region", xlim=(2800, 3000))

Create subplots for different component groups:

>>> fig, axes = plt.subplots(2, 1, figsize=(12, 8))
>>> plot1 = LoadingsPlot(loadings, feature_names=wavelengths, components=[0, 1])
>>> plot1.render(ax=axes[0])
>>> plot2 = LoadingsPlot(loadings, feature_names=wavelengths, components=[0, 1])
>>> plot2.render(ax=axes[1], xlim=(2800, 3000))
>>> plt.tight_layout()

Custom styling:

>>> plot.show(
...     title="Styled Loadings",
...     linewidth=2,
...     alpha=0.8
... )
show(figsize=(12, 4), title=None, xlabel='X-axis', ylabel='Y-axis', xlim=None, ylim=None, **kwargs) Figure[source]

Show the loadings plot with optional customization.

Parameters:
  • figsize (tuple, optional) – Figure size as (width, height) in inches. Default is (12, 4).

  • title (str, optional) – Title for the plot. If None, a default title is generated.

  • xlabel (str, optional) – X-axis label. Default is “X-axis”.

  • ylabel (str, optional) – Y-axis label. Default is “Y-axis”.

  • xlim (tuple, optional) – X-axis limits as (xmin, xmax). Default is None (auto).

  • ylim (tuple, optional) – Y-axis limits as (ymin, ymax). Default is None (auto).

  • **kwargs (Any) – Additional keyword arguments passed to the plot function.

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 plot on. If None, creates new figure and axes.

  • xlabel (str, optional) – X-axis label. Default is “Feature”.

  • ylabel (str, optional) – Y-axis label. Default is “Loading”.

  • 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 plot function.

Returns:

  • fig (Figure) – The matplotlib Figure object.

  • ax (Axes) – The matplotlib Axes object with the rendered plot.

Examples

Render on existing axes:

>>> fig, ax = plt.subplots()
>>> plot.render(ax=ax)

Create subplots:

>>> fig, axes = plt.subplots(2, 1, figsize=(12, 8))
>>> plot1.render(ax=axes[0])
>>> plot2.render(ax=axes[1])