insitupy.plotting.spatial

Contents

insitupy.plotting.spatial#

insitupy.plotting.spatial(data, keys, cells_layer=None, units_layer=None, layer=UNSET, region_tuple=UNSET, annotation_tuple=UNSET, annotations_key=None, image_key=UNSET, filter_mode=UNSET, filter_tuple=UNSET, xlim=UNSET, ylim=UNSET, spot_size=UNSET, alpha=UNSET, nan_color=UNSET, max_cols=UNSET, figsize=UNSET, subplot_width=UNSET, subplot_height=UNSET, savepath=None, save_only=False, dpi_save=300, show=True, plot_config=None, layout_config=None, data_config=None, verbose=False)#

Plot spatial omics data with optional images, annotations, and regions.

This function generates spatial scatter plots of spatially-resolved omics data from one or multiple InSituData or InSituExperiment objects. It supports categorical and continuous features, overlays images and annotations, and provides flexible configuration for plotting, layout, and saving.

Parameters:
  • data (InSituData | InSituExperiment) – Input dataset or experiment.

  • keys (str | list[str] | None) – Feature key(s) to plot (e.g., gene names or annotations). None draws only the image (image_key) and the annotations/regions without any cells, so no cell layer has to be loaded; one panel per sample. The axis limits then come from xlim/ylim, else from region_tuple, else from the image extent, else from the bounds of the drawn annotations. With origin_zero=True the coordinate origin is moved to the lower-left corner of these limits. cells_layer, units_layer, layer and the filters have no effect. Raises ValueError if nothing can be drawn at all.

  • cells_layer (str | None (default: None)) – Name of the cell layer to extract data from.

  • units_layer (str | None (default: None)) – Name of a units layer (e.g. Visium HD bins such as "square_008um", or Visium spots) to plot instead of a cell layer. Mutually exclusive with cells_layer. Unless spot_size/plot_config are given, bins are drawn as squares with the bin edge length as marker size (tiling the tissue) and spots as circles with the spot diameter. Plotting 2 um layers of a full slide is slow: use xlim/ylim or crop first. Colors of an InSituExperiment (exp.colors) are not used for units layers.

  • layer (str | None (default: UNSET)) – AnnData layer to extract values from.

  • region_tuple (tuple[str, str] | None (default: UNSET)) – Region identifier (dataset key, region name).

  • annotation_tuple (tuple[str, str | list[str] | None] | None (default: UNSET)) – Annotation overlay specifier as (key, name) where key is the annotation category and name is the specific annotation class (or a list of classes) to overlay. Pass just the key as a plain string to overlay all classes in that category.

  • annotations_key (tuple[str, str | list[str] | None] | None (default: None)) – Deprecated. Use annotation_tuple instead.

  • image_key (str | None (default: UNSET)) – Key for associated images to overlay. In an experiment, samples without this image are plotted without one (with a warning); a single sample without it raises a ValueError.

  • filter_mode (Optional[Literal['contains', 'not contains', 'starts with', 'ends with', 'is equal', 'is not', 'in', 'not in', 'greater than', 'less than', 'greater or equal', 'less or equal']] (default: UNSET)) – Mode used for filtering cells (e.g., “contains”, “greater than”).

  • filter_tuple (tuple | None (default: UNSET)) – Parameters for filtering (depends on filter_mode).

  • xlim (tuple[float, float] | None (default: UNSET)) – X-axis limits.

  • ylim (tuple[float, float] | None (default: UNSET)) – Y-axis limits.

  • spot_size (float (default: UNSET)) – Marker extent in um (diameter of circles, edge of squares), drawn at this true size at any zoom level or dpi; markers smaller than one pixel are drawn at one pixel. Default is 10.

  • alpha (float (default: UNSET)) – Transparency for plotted markers. Default is 1.0.

  • nan_color (str | None (default: UNSET)) – Color for cells with missing values (NaN) in categorical columns. If None (default), NaN cells are excluded from the plot. Has no effect on continuous columns.

  • max_cols (int | None (default: UNSET)) – Maximum number of subplot columns. Default is 4.

  • figsize (tuple[float, float] | None (default: UNSET)) – Overall figure size (width, height) in inches. Overrides subplot_width/subplot_height when provided. Default None (derived from subplot_width/subplot_height).

  • subplot_width (float | None (default: UNSET)) – Width of each individual subplot panel in inches. Used to derive the total figure width when figsize is None. Default None (falls back to 6).

  • subplot_height (float | None (default: UNSET)) – Height of each individual subplot panel in inches. Used to derive the total figure height when figsize is None. Default None (falls back to 6).

  • savepath (str | None (default: None)) – Path to save the figure (if None, figure is not saved).

  • save_only (bool (default: False)) – If True, save figure without displaying. Default is False.

  • dpi_save (int (default: 300)) – Resolution in DPI for saving the figure. Default is 300.

  • show (bool (default: True)) – Whether to display the plot. Default is True.

  • plot_config (PlotConfig (default: None)) – Plot appearance/rendering configuration (cmap, palette, spot_type, crange, …) for options with no dedicated top-level kwarg. Fields it shares with a top-level kwarg above (xlim, ylim, spot_size, alpha, nan_color) are preserved unless that kwarg is also passed explicitly - see the precedence note below.

  • layout_config (LayoutConfig (default: None)) – Figure/subplot layout configuration (header, wspace, hspace, dpi_display, …) for options with no dedicated top-level kwarg. Fields it shares with a top-level kwarg above (max_cols, figsize, subplot_width, subplot_height) follow the same precedence note.

  • data_config (DataConfig (default: None)) – Data-extraction configuration (raw, obsm_key, name_column, …) for options with no dedicated top-level kwarg. Fields it shares with a top-level kwarg above (layer, region_tuple, annotation_tuple, image_key, filter_mode, filter_tuple) follow the same precedence note.

  • verbose (bool (default: False)) – If True, print progress messages. Default is False.

Note

Precedence between a shared top-level kwarg and a caller-provided plot_config/layout_config/data_config object: the top-level kwarg wins whenever it is explicitly passed - even when the value it is given equals a config default; otherwise the config object’s value is left untouched. E.g. spatial(data, keys, plot_config=PlotConfig(spot_size=20)) keeps spot_size=20; adding spot_size=8 at the top level overrides it to 8.

Returns:

Displays and/or saves the generated spatial plot(s).

Return type:

None

Raises:
  • ValueError – If filter parameters or layout arguments are invalid.

  • ValueError – If mixed categorical and continuous values are encountered for a key.

Examples: >>> import insitupy as isp >>> isp.pl.spatial(data, keys=”GeneA”) >>> isp.pl.spatial(exp, keys=[“GeneA”, “GeneB”], image_key=”lowres”, savepath=”plots/spatial.png”)