harpy.pl.plot_sdata_genes

harpy.pl.plot_sdata_genes#

harpy.pl.plot_sdata_genes(sdata, points_name, image_name=None, channel=None, name_gene_column='gene', genes=None, palette=None, color='cornflowerblue', frac=None, size=5, alpha=1.0, crd=None, to_coordinate_system='global', render_images_kwargs=mappingproxy({}), show_kwargs=mappingproxy({}), ax=None)#

Light wrapper around spatialdata-plot to visualize gene expression from a SpatialData object.

Parameters:
  • sdata (SpatialData) – SpatialData object.

  • points_name (str) – Points element to plot from sdata.points. The associated DataFrame is expected to contain gene information in name_gene_column.

  • image_name (str | None (default: None)) – Optional image element to plot from sdata.images as background. If None, no image is rendered and channel is ignored.

  • channel (str | list[str] | None (default: None)) – Channel(s) of image_name to visualize, passed to .pl.render_images(). Ignored if image_name is None.

  • name_gene_column (str | None (default: 'gene')) – Column in the points_name that stores the gene names for each point (e.g. "gene").

  • genes (str | list[str] (default: None)) – Gene or list of genes to visualize. * If a list is provided, only points in points_name from this list (via name_gene_column) are plotted (as categories). * If None, points are plotted without gene-specific coloring and color is used instead.

  • palette (str | list[str] | None (default: None)) – Colors used to color each gene in genes. If None, defaults to scanpy’s standard color palette. Ignored if genes is None.

  • color (str (default: 'cornflowerblue')) – Color used to plot the points when genes is None. Ignored if genes is not None.

  • frac (float | None (default: None)) – Fraction of points to randomly sample for plotting. If None, all points in points_name are visualized.

  • size (int | float (default: 5)) – Size of the points.

  • alpha (float (default: 1.0)) – Transparency of the points.

  • crd (tuple[int, int, int, int] | None (default: None)) – The coordinates for the region of interest in the format (xmin, xmax, ymin, ymax), in the coordinate system to_coordinate_system.

  • to_coordinate_system (str (default: 'global')) – Coordinate system to plot.

  • render_images_kwargs (Mapping[str, Any] (default: mappingproxy({}))) – Keyword arguments passed to .pl.render_images() for rendering image_name.

  • show_kwargs (Mapping[str, Any] (default: mappingproxy({}))) – Keyword arguments passed to .pl.show().

  • ax (Axes | None (default: None)) –

    matplotlib.axes.Axes object to plot on. If None, a new axes is created by

    the underlying plotting function.

Return type:

Axes

Returns:

: matplotlib.axes.Axes object.

Examples

Plot gene expression for multiple genes on top of an image:

>>> from spatialdata.datasets import blobs
>>> import matplotlib.pyplot as plt
>>> import harpy as hp
>>>
>>> sdata = blobs()
>>> fig, ax = plt.subplots()
>>>
>>> hp.pl.plot_sdata_genes(
...     sdata,
...     points_name="blobs_points",
...     image_name="blobs_image",
...     name_gene_column="genes",
...     genes=["gene_b", "gene_a"],
...     ax=ax,
... )

Plot a single gene:

>>> fig, ax = plt.subplots()
>>> hp.pl.plot_sdata_genes(
...     sdata,
...     points_name="blobs_points",
...     image_name="blobs_image",
...     name_gene_column="genes",
...     genes="gene_b",
...     ax=ax,
... )

Plot points without categorical genes, using a single color:

>>> fig, ax = plt.subplots()
>>> hp.pl.plot_sdata_genes(
...     sdata,
...     points_name="blobs_points",
...     image_name="blobs_image",
...     name_gene_column="genes",
...     frac=0.5,
...     genes=None,
...     color="cornflowerblue",
...     ax=ax,
... )