harpy.pl.plot_sdata

Contents

harpy.pl.plot_sdata#

harpy.pl.plot_sdata(sdata, image_name, channel=None, labels_name=None, table_name=None, color=None, crd=None, to_coordinate_system='global', render_images_kwargs=mappingproxy({}), render_labels_kwargs=mappingproxy({}), show_kwargs=mappingproxy({}), ax=None)#

Light wrapper around spatialdata-plot to plot a SpatialData object.

Parameters:
  • sdata (SpatialData) – SpatialData object.

  • image_name (str) – Image element to plot from sdata.images.

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

  • labels_name (str | None (default: None)) – Labels element to plot from sdata.labels.

  • table_name (str | None (default: None)) – anndata.AnnData table from sdata.tables used to color instances of the labels_name. If specified, the labels element at labels_name should be annotated by table_name. Ignored if color is None.

  • color (str | None (default: None)) – Column from sdata[table_name].obs or name from sdata[table_name].var_names to color the instances from labels_name. A ValueError is raised when color is specified and table_name is None. Set to None if you do not want to color the labels, and only want to visualise the labels in labels_name.

  • 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().

  • render_labels_kwargs (Mapping[str, Any] (default: mappingproxy({}))) – Keyword arguments passed to .pl.render_labels(). Ignored if labels_name is None.

  • 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.

Raises:
  • ValueError – If table_name is not None and labels_name is None.

  • ValueError – If color is not None and table_name is None.

  • ValueError – If coordinate_systems`in `show_kwargs. Please pass coordinate system to plot via to_coordinate_system.

Examples

>>> from spatialdata.datasets import blobs
>>> import matplotlib.pyplot as plt
>>> from matplotlib.colors import Normalize
>>> import harpy as hp
>>>
>>> # Load example spatial dataset
>>> sdata = blobs()
>>>
>>> # Configure rendering options
>>> render_images_kwargs = {
...     "palette": ["red", "blue", "green"],
...     "scale": "scale2",
... }
>>> render_labels_kwargs = {
...     "scale": "scale2",
...     "fill_alpha": 0.8,
...     "outline_alpha": 0.3,
... }
>>> show_kwargs = {
...     "title": "my_multichannel_figure",
...     "colorbar": False,
...     "dpi": 200,
...     "figsize": (20, 20),
... }
>>>
>>> fig, ax = plt.subplots(1, 2)
>>>
>>> # Plot multichannel image with labels and table annotations
>>> hp.pl.plot_sdata(
...     sdata,
...     image_name="blobs_multiscale_image",
...     channel=[0, 1, 2],
...     labels_name="blobs_labels",
...     table_name="table",
...     color="channel_1_sum",
...     render_images_kwargs=render_images_kwargs,
...     render_labels_kwargs=render_labels_kwargs,
...     show_kwargs=show_kwargs,
...     ax=ax[0],
... )
>>>
>>> # Plot a single channel with intensity normalization and crop
>>> norm = Normalize(vmin=0.1, vmax=0.2, clip=True)
>>> render_images_kwargs = {
...     "cmap": "grey",
...     "scale": "full",
...     "norm": norm,
... }
>>> show_kwargs = {
...     "title": "my_channel_1",
...     "colorbar": False,
...     "dpi": 200,
...     "figsize": (20, 20),
... }
>>>
>>> hp.pl.plot_sdata(
...     sdata,
...     image_name="blobs_multiscale_image",
...     channel=1,
...     crd=[100, 300, 100, 300],
...     render_images_kwargs=render_images_kwargs,
...     render_labels_kwargs=render_labels_kwargs,
...     show_kwargs=show_kwargs,
...     ax=ax[1],
... )