spectrochempy.plot_scoreο
- plot_score(scores, components=(1, 2), *, ax=None, clear=True, cmap=None, color=None, color_mapping='index', show_labels=False, labels_column=None, elev=None, azim=None, marker=None, s=None, alpha=None, show=True, **kwargs)[source]ο
Plot PCA scores as a 2D or 3D scatter plot.
- Parameters:
scores (array-like or NDDataset) β Scores data with shape (n_samples, n_components). If an NDDataset, the data attribute is used.
components (tuple of int, optional) β Principal components to plot (1-based indexing). Length 2 for 2D plot, length 3 for 3D plot. Default: (1, 2).
ax (matplotlib.axes.Axes, optional) β Axes to plot on. If None, a new figure is created.
clear (bool, optional) β Whether to clear the axes before plotting. Default: True. Only used when
axis provided.cmap (str or Colormap, optional) β Colormap for coloring points. Default: preferences.colormap_sequential.
color (color or array-like, optional) β If provided, use this color for all points (single color) or as color values for each point. Overrides color_mapping.
color_mapping ({βindexβ, βlabelsβ}, optional) β Method for mapping colors to points:
"index"(default): Sequential colors by sample index."labels": Color by categorical labels from scores.y.labels. Adds a legend showing label categories.
show_labels (bool, optional) β If True, annotate each point with its label from scores.y.labels. Labels are placed intelligently using
adjustTextif available, otherwise shifted slightly from the marker position to avoid overlap. Default: False.labels_column (int, optional) β Column index in scores.y.labels to use (0-based). If None, uses column 0 for color_mapping, or last column for show_labels.
elev (float, optional) β Elevation angle (degrees) for 3D plots. If None, uses preferences.axes3d_elev.
azim (float, optional) β Azimuth angle (degrees) for 3D plots. If None, uses preferences.axes3d_azim.
marker (str, optional) β Marker style for scatter points. Passed to
ax.scatter. If None, uses matplotlib default.s (float or array-like, optional) β Marker size(s) for scatter points. Passed to
ax.scatter. If None, uses matplotlib default.alpha (float, optional) β Transparency (0-1) for scatter points. If None, fully opaque.
show (bool, optional) β Whether to display the figure. Default: True.
**kwargs β Additional keyword arguments passed to
ax.scatter.
- Returns:
matplotlib.axes.Axes β The axes containing the scatter plot.
- Raises:
ValueError β If components has invalid length (not 2 or 3). If component index exceeds available components. If color_mapping=βlabelsβ but scores has no labels. If show_labels=True but scores has no y coordinate or labels.
Examples
>>> import spectrochempy as scp >>> from spectrochempy import plot_score >>> scores = scp.random(size=(50, 5)) >>> ax = plot_score(scores, components=(1, 2), show=False)
With label-based coloring:
>>> ax = plot_score(scores, color_mapping="labels", show=False)