squidpy.pl.spatial_segment
- squidpy.pl.spatial_segment(adata, color=None, groups=None, library_id=None, library_key=None, spatial_key='spatial', img=True, img_res_key='hires', img_alpha=None, img_cmap=None, img_channel=None, seg=None, seg_key='segmentation', seg_cell_id=None, seg_contourpx=None, seg_outline=False, use_raw=None, layer=None, alt_var=None, crop_coord=None, cmap=None, palette=None, alpha=1.0, norm=None, na_color=(0, 0, 0, 0), connectivity_key=None, edges_width=1.0, edges_color='grey', library_first=True, frameon=None, wspace=None, hspace=0.25, ncols=4, outline=False, outline_color=('black', 'white'), outline_width=(0.3, 0.05), legend_loc='right margin', legend_fontsize=None, legend_fontweight='bold', legend_fontoutline=None, legend_na=True, colorbar=True, scalebar_dx=None, scalebar_units=None, title=None, axis_label=None, fig=None, ax=None, return_ax=False, figsize=None, dpi=None, save=None, scalebar_kwargs=mappingproxy({}), edges_kwargs=mappingproxy({}), **kwargs)[source]
Plot spatial omics data with segmentation masks on top.
Argument
seg_cell_id
inanndata.AnnData.obs
controls unique segmentation mask’s ids to be plotted. By default,'segmentation'
,seg_key
for the segmentation and'hires'
for the image is attempted.Use
seg_key
to display the image in the background.Use
seg_contourpx
orseg_outline
to control how the segmentation mask is displayed.
Use
library_id
to select the image. If multiplelibrary_id
are available, uselibrary_key
inanndata.AnnData.obs
to plot the subsets. Usecrop_coord
to crop the spatial plot based on coordinate boundaries.This function has few key assumptions about how coordinates and libraries are handled:
The arguments
library_key
andlibrary_id
control which dataset is plotted. If multiple libraries are present, specifying solelylibrary_key
will suffice, and all unique libraries will be plotted sequentially. To select specific libraries, use thelibrary_id
argument.The argument
color
controls which features in obs/var are plotted. They are plotted for all available/specified libraries. The argumentgroups
can be used to select categories to be plotted. This is valid only for categorical features inanndata.AnnData.obs
.If multiple
library_id
are available, arguments such assize
andcrop_coord
accept lists to selectively customize differentlibrary_id
plots. This requires that the length of such lists matches the number of unique libraries in the dataset.Coordinates are in the pixel space of the source image, so an equal aspect ratio is assumed.
The origin (0, 0) is on the top left, as is common convention with image data.
The plotted points (dots) do not have a real “size” but it is relative to their coordinate/pixel space. This does not hold if no image is plotted, then the size corresponds to points size passed to
matplotlib.axes.Axes.scatter()
.
If
anndata.AnnData.uns
['spatial']
is present, useimg_key
,seg_key
andsize_key
arguments to find values forimg
,seg
andsize
. Alternatively, these values can be passed directly viaimg
.See also
squidpy.pl.spatial_scatter()
on how to plot spatial data with overlayed data on top.
- Parameters:
adata (
AnnData
) – Annotated data object.seg_cell_id (
str
|None
) – Column inanndata.AnnData.obs
with unique segmentation mask ids. Required to filter valid segmentation masks.seg (
bool
|ndarray
[Any
,dtype
[Any
]] |Sequence
[ndarray
[Any
,dtype
[Any
]]] |None
) – Whether to plot the segmentation mask. One (or more)numpy.ndarray
can also be passed for plotting.seg_key (
str
|None
) – Key of segmentation mask inanndata.AnnData.uns
.seg_contourpx (
Optional
[int
]) – Draw contour of specified width for each segment. If None, fills entire segment, seeskimage.morphology.erosion()
.seg_outline (
bool
) – Whether to plot boundaries around segmentation masks.color (
str
|Sequence
[str
|None
] |None
) – Key for annotations inanndata.AnnData.obs
or variables/genes.groups (
str
|Sequence
[str
] |None
) – For discrete annotation incolor
, select which values to plot (other values are set to NAs).library_id (
str
|Sequence
[str
] |None
) – Name of the Z-dimension(s) that this function should be applied to. For not specified Z-dimensions, the identity function is applied.library_key (
str
|None
) – If multiple library_id, column inanndata.AnnData.obs
which stores mapping betweenlibrary_id
and obs.spatial_key (
str
) – Key inanndata.AnnData.obsm
where spatial coordinates are stored.img (
bool
|ndarray
[Any
,dtype
[Any
]] |Sequence
[ndarray
[Any
,dtype
[Any
]]] |None
) – Whether to plot the image. One (or more)numpy.ndarray
can also be passed for plotting.img_res_key (
str
|None
) – Key for image resolution, used to getimg
andscale_factor
from'images'
and'scalefactors'
entries for this library.img_alpha (
float
|None
) – Alpha value for the underlying image.image_cmap – Colormap for the image, see
matplotlib.colors.Colormap
.img_channel (
int
|list
[int
] |None
) – To select which channel to plot (all by default).use_raw (
bool
|None
) – If True, useanndata.AnnData.raw
.layer (
str
|None
) – Key inanndata.AnnData.layers
or None foranndata.AnnData.X
.alt_var (
str
|None
) – Which column to use inanndata.AnnData.var
to select alternativevar_name
.size – Size of the scatter point/shape. In case of
spatial_shape
it represents to the scaling factor for shape (accessed viasize_key
). In case ofspatial_point
, it represents thesize
argument inmatplotlib.pyplot.scatter()
.size_key – Key of of pixel size of shapes to be plotted, stored in
anndata.AnnData.uns
. Only needed forspatial_shape
.scale_factor – Scaling factor used to map from coordinate space to pixel space. Found by default if
library_id
andimg_key
can be resolved. Otherwise, defaults to 1.crop_coord (
tuple
[int
,int
,int
,int
] |Sequence
[tuple
[int
,int
,int
,int
]] |None
) – Coordinates to use for cropping the image (left, right, top, bottom). These coordinates are expected to be in pixel space (same asspatial
) and will be transformed byscale_factor
. If not provided, image is automatically cropped to bounds ofspatial
, plus a border.cmap (
str
|Colormap
|None
) – Colormap for continuous annotations, seematplotlib.colors.Colormap
.palette (
str
|ListedColormap
|None
) – Palette for discrete annotations, seematplotlib.colors.Colormap
.alpha (
float
) – Alpha value for scatter point/shape.norm (
Normalize
|Sequence
[Normalize
] |None
) – Colormap normalization for continuous annotations, seematplotlib.colors.Normalize
.na_color (
str
|tuple
[float
,...
]) – Color to be used for NAs values, if present.connectivity_key (
str
|None
) – Key for neighbors graph to plot. Default is:anndata.AnnData.obsp
['spatial_connectivities']
.edges_width (
float
) – Width of the edges. Only used whenconnectivity_key != None
.edges_color (
str
|Sequence
[str
] |Sequence
[float
]) – Color of the edges.library_first (
bool
) – If multiple libraries are plotted, set the plotting order with respect tocolor
.frameon (
bool
|None
) – If True, draw a frame around the panels.hspace (
float
) – Height space between panels.ncols (
int
) – Number of panels per row.outline (
bool
) – If True, a thin border around points/shapes is plotted.legend_loc (
str
|None
) – Location of the legend, seematplotlib.legend.Legend
.legend_fontsize (
Union
[int
,float
,Literal
['xx-small'
,'x-small'
,'small'
,'medium'
,'large'
,'x-large'
,'xx-large'
],None
]) – Font size of the legend, seematplotlib.text.Text.set_fontsize()
.legend_fontweight (
Union
[int
,Literal
['light'
,'normal'
,'medium'
,'semibold'
,'bold'
,'heavy'
,'black'
]]) – Font weight of the legend, seematplotlib.text.Text.set_fontweight()
.legend_fontoutline (
int
|None
) – Font outline of the legend, seematplotlib.patheffects.withStroke
.legend_na (
bool
) – Whether to show NA values in the legend.colorbar (
bool
) – Whether to show the colorbar, seematplotlib.pyplot.colorbar()
.scalebar_dx (
float
|Sequence
[float
] |None
) – Size of one pixel in units specified byscalebar_units
.scalebar_units (
str
|Sequence
[str
] |None
) – Units ofscalebar_dx
.axis_label (
str
|Sequence
[str
] |None
) – Panel axis labels.fig (
Figure
|None
) – Optionalmatplotlib.figure.Figure
to use.ax (
Axes
|Sequence
[Axes
] |None
) – Optionalmatplotlib.axes.Axes
to use.return_ax (
bool
) – Whether to returnmatplotlib.axes.Axes
object(s).figsize (
tuple
[float
,float
] |None
) – Size of the figure in inches.scalebar_kwargs (
Mapping
[str
,Any
]) – Keyword arguments formatplotlib_scalebar.ScaleBar()
.edges_kwargs (
Mapping
[str
,Any
]) – Keyword arguments fornetworkx.draw_networkx_edges()
.kwargs (
Any
) – Keyword arguments formatplotlib.pyplot.scatter()
ormatplotlib.pyplot.imshow()
.
- Return type:
- Returns:
: Nothing, just plots the figure and optionally saves the plot.