alphapepttools.pl.plot_pca_loadings_2d

alphapepttools.pl.plot_pca_loadings_2d#

alphapepttools.pl.plot_pca_loadings_2d(data, ax, dim_space='obs', embeddings_name=None, method='pca', pc_x=1, pc_y=2, nfeatures=20, *, add_labels=True, add_lines=False, scatter_kwargs=None)#

2D loadings plot showing top features contributing to two principal components.

Creates a scatter plot displaying the first two principal component loadings against each other. Loadings indicate how much each feature (gene/protein) contributes to each PC. The plot shows all features used in the PCA as grey points, with the top N features (by absolute loading value) highlighted in blue. Optionally, labels can be added to the top features.

Parameters:
  • data (AnnData | DataFrame) – AnnData to plot.

  • ax (Axes) – Matplotlib axes object to plot on.

  • dim_space (str (default: 'obs')) – The dimension space used in PCA. Can be either “obs” (default) for sample projection or “var” for feature projection. By default “obs”.

  • embeddings_name (str | None (default: None)) – The custom embeddings name used in PCA. If None, uses default naming convention. By default None.

  • method (Literal['pca', 'bpca'] (default: 'pca')) – The method used for dimensionality reduction. Options are “pca” or “bpca” with “pca” as the default. This is used to construct the default keys if embeddings_name is None.

  • pc_x (int (default: 1)) – The PC principal component index to plot on the x axis, by default 1. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).

  • pc_y (int (default: 2)) – The principal component index to plot on the y axis, by default 2. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).

  • nfeatures (int (default: 20)) – The number of top absolute loadings features to label from each component, by default 20

  • add_labels (bool (default: True)) – Whether to add feature labels of the top nfeatures loadings. by default True.

  • add_lines (bool (default: False)) – If True, draw lines connecting the origin (0,0) to the points representing the top nfeatures loadings. Default is False.

  • scatter_kwargs (dict | None (default: None)) – Additional keyword arguments for the matplotlib scatter function. By default None.

Return type:

None

Examples

Basic 2D PCA loadings plot:

fig, ax = plt.supplots()
Plots.plot_pca_loadings_2d(
    data=adata,
    ax=ax,
    pc_x=1,
    pc_y=2,
    nfeatures=20,
    add_labels=True,
    add_lines=True,
    scatter_kwargs=None,
)

Notes

  • PCA must be run on the AnnData object before calling this function

  • Features are ranked by absolute loading value (magnitude, not sign)

  • X and Y axes show loading values for the specified principal components

  • dim_space=”obs” shows feature loadings (most common - which proteins/genes matter)

  • dim_space=”var” shows sample loadings (which samples matter)

  • This is a convenience wrapper around scatter() with automatic loadings data extraction