elbow#

API pages include interactive (HTML) plots that would possibly not render correctly on a mobile device.

elbow(data: AnnData, n_pcs: int | None = None, *, mapping: FeatureSpec | None = None, component_column: str = 'Principal Component', variance_column: str = 'Variance Ratio', color: str = '#1f1f1f', size: float = 4.0, line_size: float = 1, line_type: str = 'dashed', label: bool = False, label_size: float = 4.0, label_every: int = 5, pca_key: str = 'pca', **geom_kwargs) PlotSpec#

Elbow Plot.

Parameters:
  • data (AnnData) – The single-cell data object. Must have PCA results computed beforehand.

  • n_pcs (int | None, default None) – Number of principal components to display. If None, shows all available components.

  • mapping (FeatureSpec | None, default None) – Additional aesthetic mappings for the plot, the result of aes().

  • component_column (str, default 'Principal Component') – The name to give to the component (x-axis) column in the dataframe.

  • variance_column (str, default 'Variance Ratio') – The name to give to the variance ratio (y-axis) column in the dataframe.

  • color (str, default '#1f1f1f') – Color of the points and connecting line.

  • size (float, default 4.0) – Size of the points.

  • line_size (float, default 1) – Size of the connecting line.

  • line_type (str, default 'dashed') – Linetype of the connecting line (e.g., ‘solid’, ‘dashed’, ‘dotted’).

  • label (bool, default False) – If True, draw a vertical PC{n} label on each selected component using geom_label.

  • label_size (float, default 4.0) – Text size for the component labels (used when label=True).

  • label_every (int, default 5) – Only components whose number is a multiple of this value are labeled (used when label=True). For example, 5 labels PC5, PC10, PC15, …

  • pca_key (str, default 'pca') – Specific key holding the PCA results.

  • **geom_kwargs – Additional parameters for the geom_point layer. For more information on geom_point parameters, see: https://lets-plot.org/python/pages/api/lets_plot.geom_point.html

Returns:

PlotSpec – Elbow plot.

Raises:
  • UnsupportedDataTypeError – If data is not a supported single-cell data object.

  • ValueError – If PCA results are not available on data, or if n_pcs exceeds the number of available components.

Notes

Plots the variance ratio explained by each principal component, helping identify how many components to retain.

Examples

A simple elbow plot.

import scanpy as sc
from lets_plot import *

import cellestial as cl

data = cl.datasets.pbmc3k(cache_directory="data")
sc.tl.pca(data)

cl.elbow(data)

Limit components and customize.

cl.elbow(data, n_pcs=20, color="#0f4f8f", size=4)