squidpy.pl.spatial_scatter
- squidpy.pl.spatial_scatter(adata, shape=None, 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, use_raw=None, layer=None, alt_var=None, size=None, size_key='spot_diameter_fullres', scale_factor=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 data overlayed on top.
The plotted shapes (circles, squares or hexagons) have a real “size” with respect to their coordinate space, which can be specified via the
sizeorsize_keyargument.Use
img_keyto display the image in the background.Use
library_idto select the image. By default,'hires'is attempted.Use
img_alpha,img_cmapandimg_channelto control how it is displayed.Use
sizeto scale the size of the shapes plotted on top.
If no image is plotted, it defaults to a scatter plot, see
matplotlib.axes.Axes.scatter().Use
library_idto select the image. If multiplelibrary_idare available, uselibrary_keyinanndata.AnnData.obsto plot the subsets. Usecrop_coordto crop the spatial plot based on coordinate boundaries.This function has few key assumptions about how coordinates and libraries are handled:
The arguments
library_keyandlibrary_idcontrol which dataset is plotted. If multiple libraries are present, specifying solelylibrary_keywill suffice, and all unique libraries will be plotted sequentially. To select specific libraries, use thelibrary_idargument.The argument
colorcontrols which features in obs/var are plotted. They are plotted for all available/specified libraries. The argumentgroupscan be used to select categories to be plotted. This is valid only for categorical features inanndata.AnnData.obs.If multiple
library_idare available, arguments such assizeandcrop_coordaccept lists to selectively customize differentlibrary_idplots. 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_keyandsize_keyarguments to find values forimg,segandsize. Alternatively, these values can be passed directly viaimg.See also
squidpy.pl.spatial_segment()on how to plot spatial data with segmentation masks on top.
- Parameters:
adata (
AnnData) – Annotated data object.shape (
Optional[Literal['circle','square','hex']]) – Whether to plot scatter plot of points or regular polygons.color (
str|Sequence[str|None] |None) – Key for annotations inanndata.AnnData.obsor 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.obswhich stores mapping betweenlibrary_idand obs.spatial_key (
str) – Key inanndata.AnnData.obsmwhere spatial coordinates are stored.img (
bool|ndarray[tuple[Any,...],dtype[Any]] |Sequence[ndarray[tuple[Any,...],dtype[Any]]] |None) – Whether to plot the image. One (or more)numpy.ndarraycan also be passed for plotting.img_res_key (
str|None) – Key for image resolution, used to getimgandscale_factorfrom'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.layersor None foranndata.AnnData.X.alt_var (
str|None) – Which column to use inanndata.AnnData.varto select alternativevar_name.size (
float|Sequence[float] |None) – Size of the scatter point/shape. In case ofspatial_shapeit represents to the scaling factor for shape (accessed viasize_key). In case ofspatial_point, it represents thesizeargument inmatplotlib.pyplot.scatter().size_key (
str|None) – Key of of pixel size of shapes to be plotted, stored inanndata.AnnData.uns. Only needed forspatial_shape.scale_factor (
float|Sequence[float] |None) – Scaling factor used to map from coordinate space to pixel space. Found by default iflibrary_idandimg_keycan 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.Figureto use.ax (
Axes|Sequence[Axes] |None) – Optionalmatplotlib.axes.Axesto use.return_ax (
bool) – Whether to returnmatplotlib.axes.Axesobject(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.drawing.nx_pylab.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.