Plotting (gd.pl)¶
The plotting module provides functions for visualizing GEDI results, including embeddings, convergence metrics, and feature plots.
Embedding Plots¶
Plot GEDI embedding colored by various attributes. |
|
Plot GEDI UMAP embedding. |
|
Plot GEDI PCA embedding. |
- gedi2py.plotting.embedding(adata, *, basis='X_gedi_umap', color=None, layer=None, size=None, alpha=1.0, title=None, legend_loc='right margin', legend_fontsize=10, cmap='viridis', palette=None, frameon=False, ncols=4, wspace=0.4, show=None, save=None, ax=None, return_fig=False, **kwargs)[source]¶
Plot GEDI embedding colored by various attributes.
- Parameters:
adata (
AnnData) – Annotated data matrix.basis (
str, default:'X_gedi_umap') – Key inadata.obsmfor the embedding coordinates. Defaults toX_gedi_umap.color (
Union[str,Sequence[str],None], default:None) – Key(s) for annotation inadata.obsor gene names to color by. Can be a single key or a list of keys.layer (
str|None, default:None) – Layer to use for gene expression values.size (
float|None, default:None) – Point size. IfNone, automatically determined.alpha (
float, default:1.0) – Point transparency (0-1).title (
Union[str,Sequence[str],None], default:None) – Panel title(s). IfNone, uses thecolorkey(s).legend_loc (
str, default:'right margin') – Location of the legend. Options:'right margin','on data','best','upper right', etc.legend_fontsize (
int|float, default:10) – Font size for legend text.cmap (
str, default:'viridis') – Colormap for continuous variables.palette (
Union[str,Sequence[str],None], default:None) – Color palette for categorical variables.frameon (
bool, default:False) – Whether to show axis frame.ncols (
int, default:4) – Number of columns for multi-panel plots.wspace (
float, default:0.4) – Width space between panels.show (
bool|None, default:None) – IfTrue, show the figure. IfNone, defaults toTrueunlesssaveis specified.ax (
Axes|None, default:None) – Pre-existing axes for the plot. Only valid whencoloris a single key.return_fig (
bool, default:False) – IfTrue, return the figure object.**kwargs – Additional arguments passed to
matplotlib.pyplot.scatter.
- Returns:
If
return_fig=True: Returns the Figure objectIf
axis provided: Returns the Axes objectOtherwise: Returns
None
- Return type:
Depending on ``return_fig``andax
Examples
>>> import gedi2py as gd >>> gd.tl.gedi(adata, batch_key="sample", n_latent=10) >>> gd.tl.umap(adata) >>> gd.pl.embedding(adata, color="cell_type") >>> gd.pl.embedding(adata, color=["batch", "cell_type", "total_counts"])
Example
import gedi2py as gd import matplotlib.pyplot as plt # Basic embedding plot gd.pl.embedding(adata, color="cell_type") # Multiple colors gd.pl.embedding(adata, color=["sample", "cell_type"], ncols=2) # Customize appearance gd.pl.embedding( adata, basis="X_gedi_umap", color="cell_type", size=10, alpha=0.8, palette="tab20", legend_loc="right margin", frameon=False, ) plt.show()
- gedi2py.plotting.umap(adata, *, color=None, key='gedi', **kwargs)[source]¶
Plot GEDI UMAP embedding.
Convenience wrapper for
embedding()withbasis='X_{key}_umap'.See
embedding()for full parameter documentation.Convenience wrapper for
embedding(basis="X_gedi_umap", ...).gd.pl.umap(adata, color="sample")
- gedi2py.plotting.pca(adata, *, color=None, components=(1, 2), key='gedi', **kwargs)[source]¶
Plot GEDI PCA embedding.
- Parameters:
adata (
AnnData) – Annotated data matrix.color (
Union[str,Sequence[str],None], default:None) – Key(s) for annotation to color by.components (
tuple[int,int], default:(1, 2)) – Which principal components to plot (1-indexed).key (
str, default:'gedi') – Key prefix for GEDI results.**kwargs – Additional arguments passed to
embedding().
- Return type:
:param See
embedding()for full parameter documentation.:Convenience wrapper for
embedding(basis="X_gedi_pca", ...).gd.pl.pca(adata, color="sample")
Convergence Plots¶
Plot GEDI training convergence. |
|
Plot GEDI training loss (negative log-likelihood). |
- gedi2py.plotting.convergence(adata, *, which='all', key='gedi', log_scale=True, title=None, figsize=None, show=None, save=None, ax=None, return_fig=False)[source]¶
Plot GEDI training convergence.
Visualizes how model parameters evolved during training to assess convergence. Available metrics:
sigma2: Noise variance (should stabilize)dZ: Change in metagenes Z per iterationdA: Change in pathway coefficients A (if using pathway priors)do: Change in offsets o
- Parameters:
adata (
AnnData) – Annotated data matrix with GEDI results.which (
Literal['sigma2','dZ','dA','do','all'], default:'all') – Which convergence metric(s) to plot: -"sigma2": Only noise variance -"dZ": Only metagene changes -"dA": Only pathway coefficient changes -"do": Only offset changes -"all": All available metrics (default)key (
str, default:'gedi') – Key inadata.unswhere GEDI results are stored.log_scale (
bool, default:True) – IfTrue, use log scale for y-axis (except sigma2).figsize (
tuple[float,float] |None, default:None) – Figure size (width, height) in inches.show (
bool|None, default:None) – IfTrue, show the figure.ax (
Axes|None, default:None) – Pre-existing axes for the plot. Only valid for single metric.return_fig (
bool, default:False) – IfTrue, return the figure object.
- Returns:
Depending on ``return_fig``
- Return type:
Figure,Axes, orNone.
Examples
>>> import gedi2py as gd >>> gd.tl.gedi(adata, batch_key="sample", n_latent=10) >>> gd.pl.convergence(adata) >>> gd.pl.convergence(adata, which="sigma2", log_scale=False)
Example
# Plot all convergence metrics gd.pl.convergence(adata, which="all") # Plot specific metric gd.pl.convergence(adata, which="sigma2", log_scale=True) # Save figure gd.pl.convergence(adata, save="convergence.png")
Available Metrics
sigma2: Noise variancedZ: Change in Z matrixdA: Change in A matrixdo: Change in offsetsall: All metrics in subplots
- gedi2py.plotting.loss(adata, *, key='gedi', title='Training Loss', figsize=(6, 4), show=None, save=None, ax=None, return_fig=False)[source]¶
Plot GEDI training loss (negative log-likelihood).
- Parameters:
adata (
AnnData) – Annotated data matrix with GEDI results.key (
str, default:'gedi') – Key inadata.unswhere GEDI results are stored.title (
str, default:'Training Loss') – Plot title.figsize (
tuple[float,float], default:(6, 4)) – Figure size.show (
bool|None, default:None) – IfTrue, show the figure.return_fig (
bool, default:False) – IfTrue, return the figure.
- Returns:
Depending on ``return_fig``
- Return type:
Figure,Axes, orNone.
Plot the loss/objective function over iterations.
gd.pl.loss(adata, log_scale=False)
Feature Plots¶
Plot gene expression on GEDI embedding. |
|
Plot top genes per GEDI metagene (latent factor). |
|
Plot gene dispersion from GEDI model. |
|
Plot variance explained by GEDI components. |
- gedi2py.plotting.features(adata, features, *, basis='X_gedi_umap', layer=None, cmap='viridis', vmin=None, vmax=None, size=None, alpha=1.0, ncols=4, figsize=None, show=None, save=None, return_fig=False)[source]¶
Plot gene expression on GEDI embedding.
- Parameters:
adata (
AnnData) – Annotated data matrix.features (
Union[str,Sequence[str]]) – Gene name(s) to plot.basis (
str, default:'X_gedi_umap') – Key inadata.obsmfor embedding coordinates.layer (
str|None, default:None) – Layer to use for expression values. IfNone, usesadata.X.cmap (
str, default:'viridis') – Colormap for expression values.vmin (
float|None, default:None) – Minimum value for color scale.vmax (
float|None, default:None) – Maximum value for color scale.size (
float|None, default:None) – Point size. IfNone, automatically determined.alpha (
float, default:1.0) – Point transparency.ncols (
int, default:4) – Number of columns for multi-panel plots.figsize (
tuple[float,float] |None, default:None) – Figure size.show (
bool|None, default:None) – IfTrue, show the figure.return_fig (
bool, default:False) – IfTrue, return the figure.
- Return type:
Figure if ``return_fig=True``,else ``None`.`
Examples
>>> import gedi2py as gd >>> gd.pl.features(adata, ["CD4", "CD8A", "FOXP3"])
Example
# Plot marker genes gd.pl.features( adata, features=["CD3D", "CD14", "MS4A1", "NKG7"], ncols=2, ) # Use different colormap gd.pl.features( adata, features="CD3D", cmap="RdBu_r", vmin=-2, vmax=2, )
- gedi2py.plotting.metagenes(adata, *, n_genes=10, key='gedi', figsize=None, show=None, save=None, return_fig=False)[source]¶
Plot top genes per GEDI metagene (latent factor).
Shows a heatmap of the top genes with highest loadings for each latent factor in the Z matrix.
- Parameters:
adata (
AnnData) – Annotated data matrix with GEDI results.n_genes (
int, default:10) – Number of top genes to show per metagene.key (
str, default:'gedi') – Key prefix for GEDI results.figsize (
tuple[float,float] |None, default:None) – Figure size.show (
bool|None, default:None) – IfTrue, show the figure.return_fig (
bool, default:False) – IfTrue, return the figure.
- Return type:
Figure if ``return_fig=True``,else ``None`.`
Examples
>>> import gedi2py as gd >>> gd.tl.gedi(adata, batch_key="sample", n_latent=10) >>> gd.pl.metagenes(adata, n_genes=15)
Visualize metagene patterns across cells.
# Plot first 4 metagenes gd.pl.metagenes(adata, metagenes=[0, 1, 2, 3], ncols=2)
- gedi2py.plotting.dispersion(adata, *, key='gedi', n_top=20, log_scale=True, figsize=(10, 6), show=None, save=None, return_fig=False)[source]¶
Plot gene dispersion from GEDI model.
Visualizes the dispersion (variance/mean ratio) of genes, which can help identify highly variable genes.
- Parameters:
adata (
AnnData) – Annotated data matrix with GEDI dispersion computed.key (
str, default:'gedi') – Key prefix for GEDI results.n_top (
int, default:20) – Number of top dispersed genes to label.log_scale (
bool, default:True) – IfTrue, use log scale for axes.figsize (
tuple[float,float], default:(10, 6)) – Figure size.show (
bool|None, default:None) – IfTrue, show the figure.return_fig (
bool, default:False) – IfTrue, return the figure.
- Return type:
Figure if ``return_fig=True``,else ``None`.`
Examples
>>> import gedi2py as gd >>> gd.tl.dispersion(adata) >>> gd.pl.dispersion(adata, n_top=30)
Plot gene dispersion vs mean expression.
gd.pl.dispersion(adata, highlight_hvg=True)
- gedi2py.plotting.variance_explained(adata, *, key='gedi', n_components=None, figsize=(8, 4), show=None, save=None, return_fig=False)[source]¶
Plot variance explained by GEDI components.
Shows the proportion of variance explained by each latent factor based on the SVD singular values.
- Parameters:
adata (
AnnData) – Annotated data matrix with GEDI SVD computed.key (
str, default:'gedi') – Key prefix for GEDI results.n_components (
int|None, default:None) – Number of components to show. IfNone, shows all.figsize (
tuple[float,float], default:(8, 4)) – Figure size.show (
bool|None, default:None) – IfTrue, show the figure.return_fig (
bool, default:False) – IfTrue, return the figure.
- Return type:
Figure if ``return_fig=True``,else ``None`.`
Examples
>>> import gedi2py as gd >>> gd.tl.svd(adata) >>> gd.pl.variance_explained(adata)
Plot variance explained by each latent factor.
# Bar plot of variance explained gd.pl.variance_explained(adata) # Cumulative variance gd.pl.variance_explained(adata, cumulative=True)
Common Parameters¶
Most plotting functions share these parameters:
Parameter |
Type |
Description |
|---|---|---|
show |
bool | None |
Show the plot (default: True in interactive mode) |
save |
str | None |
Path to save the figure |
ax |
Axes | None |
Matplotlib axes to plot on |
return_fig |
bool |
Return the Figure object instead of showing |
Using with Matplotlib
import matplotlib.pyplot as plt
# Create custom figure
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
gd.pl.umap(adata, color="sample", ax=axes[0], show=False)
gd.pl.umap(adata, color="cell_type", ax=axes[1], show=False)
gd.pl.convergence(adata, which="sigma2", ax=axes[2], show=False)
plt.tight_layout()
plt.savefig("analysis.png", dpi=300)
plt.show()