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, legend=True, radius=None, crd=None, to_coordinate_system='global', vmin=None, vmax=None, vmin_img=None, vmax_img=None, colorbar=False, shapes_layer_filtered=None, img_title=False, shapes_title=False, channel_title=True, aspect='equal', figsize=None, fig_kwargs=mappingproxy({}), output=None)#
Plots a SpatialData object.
This function support plotting of a raster (
img_layerorlabels_layer), together with ashapes_layerrespresenting (cell) boundaries. These shapes can be colored if atable_layerand acolumnis specified.The number of provided
img_layerorlabels_layerandshapes_layershould be equal if both are iterables and if their length is greater than 1.Examples
1. For
img_layer=['raw_image', 'clahe']andshapes_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']andshapes_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']andshapes_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']andshapes_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']andshapes_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 (
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 (
str|Iterable[str] |None(default:None)) – Labels layer(s) to be plotted. Displayed as columns in the plot, if multiple are provided.shapes_layer (
str|Iterable[str] |None(default:None)) – Specifies which shapes to plot. Default is ‘segmentation_mask_boundaries’. If set to None, no shapes_layer is plot. Can be colored bycolumninsdata.tables[table_layer].obsorsdata.tables[table_layer].var. For this the index of theshapes_layerwill be matched withsdata.tables[table_layer].obs[_INSTANCE_KEY]for those observations for whichsdata.tables[table_layer].obs[_REGION_KEY]equalsregion(ifregionis notNone).table_layer (
str|None(default:None)) – Table layer to be plotted (i.e. to base cell colors on) ifcolumnis specified.column (
str|None(default:None)) – Column insdata.tables[table_layer].obsor name insdata.tables[table_layer].var.indexto base cell colors on. If none provided, default color is used.region (
str|None(default:None)) – Iftable_layerandcolumnis specified, this specifies the region insdata.tables[table_layer]to be plotted (viasdata.tables[table_layer].obs[_REGION_KEY]).cmap (
str|None(default:'magma')) – Colormap for column. Ignored if column is None, or if column + “_colors” is insdata.tables[table_layer].uns.linewidth (
float(default:1.0)) – linewidth of the shapes layer to be plotted.channel (
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 (
float|None(default:None)) – The z_slice to visualize in case of 3D (c,z,y,x) image/polygons. If no z_slice is specified andimg_layerorlabels_layeris 3D, a max projection along the z-axis will be performed. If no z_slice is specified andshapes_layeris 3D, all polygons in all z-stacks will be plotted.alpha (
float(default:0.5)) – Transparency level for the cells, given by the alpha parameter of matplotlib.legend (
bool(default:True)) – Whether to plot a legend. Ignored if column isNone.radius (
str|None(default:None)) – Column in theshapes_layerspecifying the radius. The radius will be applied usinggeometry.bufferbefore plottingshapes_layer. Useful when thegeometryof theshapes_layercontains points instead of polygons.crd (
tuple[int,int,int,int] |None(default:None)) – The coordinates for the region of interest in the format(xmin, xmax, ymin, ymax). IfNone, the entire image is considered, by defaultNone.to_coordinate_system (
str(default:'global')) – Coordinate system to plot.vmin (
float|None(default:None)) – Lower bound for color scale for continuous data (i.e. a column). Given as a percentile. Ignored if column is None.vmax (
float|None(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 (
float|None(default:None)) – Lower bound for plotting ofimg_layerorlabels_layer.vmax_img (
float|None(default:None)) – Upper bound for plotting ofimg_layerorlabels_layer.colorbar (
bool(default:False)) – Whether to add a colorbar for raster data.shapes_layer_filtered (
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 (
tuple[int,int] |None(default:None)) – Size of the figure for plotting, passed to.pyplot.figure. If not provided, a default size is used based on the number of columns and rows.fig_kwargs (
Mapping[str,Any] (default:mappingproxy({}))) – Keyword arguments passed to the.pyplot.figurecall. E.g.dpi.output (
str|Path|None(default:None)) – Path to save the plot. If not provided, plot will be displayed.
- Raises:
ValueError – If both
img_layerandlabels_layerare specified.ValueError – If
img_layerorlabels_layeris specified, and they are not found insdata.imagesrespectivelysdata.labels.ValueError – If z_slice is specified, and it is not a z_slice in specified
img_layerorlabels_layer.ValueError – If a
columnis specified, but notable_layer.ValueError – If
table_layeris specified, buttable_layeris not a table insdata.tables.ValueError – If
sdata.tables[table_layer].obs[_REGION_KEY].cat.categoriescontains more than on element, butregionis not specified.ValueError – If both
table_layer,columnandregionare specified, butregionis not insdata.tables[table_layer].obs[_REGION_KEY].cat.categories.
- Return type:
None
Notes
This function offers advanced visualization options for
sdatawith support for multiple image layers, labels layers shape layers, and channels.Either
img_layerorlabels_layershould be specified, not both.
See also
sparrow.pl.plot