sparrow.pl.plot_shapes

Contents

sparrow.pl.plot_shapes#

sparrow.pl.plot_shapes(sdata, img_layer=None, labels_layer=None, shapes_layer=None, table_layer=None, column=None, region=None, cmap='magma', linewidth=1.0, channel=None, z_slice=None, alpha=0.5, crd=None, to_coordinate_system='global', vmin=None, vmax=None, vmin_img=None, vmax_img=None, shapes_layer_filtered=None, img_title=False, shapes_title=False, channel_title=True, aspect='equal', figsize=None, output=None)#

Plot shapes and/or images/labels from a SpatialData object.

The number of provided img_layer or labels_layer and shapes_layer should be equal if both are iterables and if their length is greater than 1.

Examples

1. For img_layer=['raw_image', 'clahe'] and shapes_layer=['segmentation_mask_boundaries', 'expanded_cells20']: Subplots: - Column 1: ‘raw_image’ with ‘segmentation_mask_boundaries’ - Column 2: ‘clahe’ with ‘expanded_cells20’

2. For img_layer=['raw_image', 'clahe'] and shapes_layer='segmentation_mask_boundaries': Subplots: - Column 1: ‘raw_image’ with ‘segmentation_mask_boundaries’ - Column 2: ‘clahe’ with ‘segmentation_mask_boundaries’

3. For img_layer=['raw_image', 'clahe'] and shapes_layer=['segmentation_mask_boundaries'] (which behaves the same as the previous example): Subplots: - Column 1: ‘raw_image’ with ‘segmentation_mask_boundaries’ - Column 2: ‘clahe’ with ‘segmentation_mask_boundaries’

4. For img_layer=['raw_image'] and shapes_layer=['segmentation_mask_boundaries', 'expanded_cells20' ]: Subplots: - Column 1: ‘raw_image’ with ‘segmentation_mask_boundaries’ - Column 2: ‘raw_image’ with ‘expanded_cells20’

5. For img_layer=['raw_image', 'clahe'] and shapes_layer=None: Subplots: - Column 1: ‘raw_image’ - Column 2: ‘clahe’

When multiple channels are supplied as an Iterable, they will be displayed as rows in the image

Parameters:
  • sdata (SpatialData) – Data containing spatial information for plotting.

  • img_layer (Union[str, Iterable[str], None] (default: None)) – Image layer(s) to be plotted. If not provided, and labels_layer is also not provided, the last added image layer is plotted. Displayed as columns in the plot, if multiple are provided.

  • labels_layer (Union[str, Iterable[str], None] (default: None)) – Labels layer(s) to be plotted. Displayed as columns in the plot, if multiple are provided.

  • shapes_layer (Union[str, Iterable[str], None] (default: None)) – Specifies which shapes to plot. If set to None, no shapes_layer is plotted. Displayed as columns in the plot, if multiple are provided.

  • table_layer (Optional[str] (default: None)) – Table layer to be plotted (i.e. to base cell colors on) if column is specified.

  • column (Optional[str] (default: None)) – Column in sdata.tables[table_layer].obs or name in sdata.tables[table_layer].var.index to base cell colors on. If none provided, default color is used.

  • region (Optional[str] (default: None)) – If table_layer and column is specified, this specifies the region in sdata.tables[table_layer] to be plotted (via sdata.tables[table_layer].obs[_REGION_KEY]).

  • cmap (str | None (default: 'magma')) – Colormap for column. Ignored if column is None, or if column + “_colors” is in sdata.tables[table_layer].uns.

  • linewidth (float (default: 1.0)) – linewidth of the shapes layer to be plotted.

  • channel (Union[int, str, Iterable[int], Iterable[str], None] (default: None)) – Channel(s) to be displayed from the image. Displayed as rows in the plot. If channel is None, get the number of channels from the first img_layer given as input. Ignored if img_layer is None and labels_layer is specified.

  • z_slice (Optional[float] (default: None)) – The z_slice to visualize in case of 3D (c,z,y,x) image/polygons. If no z_slice is specified and img_layer or labels_layer is 3D, a max projection along the z-axis will be performed. If no z_slice is specified and shapes_layer is 3D, all polygons in all z-stacks will be plotted.

  • crd (Optional[tuple[int, int, int, int]] (default: None)) – The coordinates for the region of interest in the format (xmin, xmax, ymin, ymax). If None, the entire image is considered, by default None.

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

  • vmin (Optional[float] (default: None)) – Lower bound for color scale for continuous data (i.e. a column). Given as a percentile. Ignored if column is None.

  • vmax (Optional[float] (default: None)) – Upper bound for color scale for continuous data (i.e. a column). Given as a percentile. Ignored if column is None.

  • vmin_img (Optional[float] (default: None)) – Lower bound for plotting of img_layer or labels_layer.

  • vmax_img (Optional[float] (default: None)) – Upper bound for plotting of img_layer or labels_layer.

  • shapes_layer_filtered (Union[str, Iterable[str], None] (default: None)) – Extra shapes layers to plot. E.g. shapes filtered out in previous preprocessing steps.

  • img_title (bool (default: False)) – A flag indicating whether the image layer’s name should be added to the title of the plot.

  • shapes_title (bool (default: False)) – A flag indicating whether the shapes layer’s name should be added to the title of the plot.

  • channel_title (bool (default: True)) – A flag indicating whether the channel’s name should be added to the title of the plot. Ignored if img_layer is None and labels_layer is specified.

  • aspect (str (default: 'equal')) – Aspect ratio for the plot.

  • figsize (Optional[tuple[int, int]] (default: None)) – Size of the figure for plotting. If not provided, a default size is used based on the number of columns and rows.

  • output (Union[str, Path, None] (default: None)) – Path to save the plot. If not provided, plot will be displayed.

Raises:
  • ValueError – If both img_layer and labels_layer are specified.

  • ValueError – If img_layer or labels_layer is specified, and they are not found in sdata.images respectively sdata.labels.

  • ValueError – If z_slice is specified, and it is not a z_slice in specified img_layer or labels_layer.

  • ValueError – If a column is specified, but no table_layer.

  • ValueError – If table_layer is specified, but table_layer is not a table in sdata.tables.

  • ValueError – If sdata.tables[table_layer].obs[_REGION_KEY].cat.categories contains more than on element, but region is not specified.

  • ValueError – If both table_layer, column and region are specified, but region is not in sdata.tables[table_layer].obs[_REGION_KEY].cat.categories.

Return type:

None

Notes

  • This function offers advanced visualization options for sdata with support for multiple image layers, labels layers shape layers, and channels.

  • Either img_layer or labels_layer should be specified, not both.