scanpy.pl.spatial
- scanpy.pl.spatial(adata, *, color=None, gene_symbols=None, use_raw=None, sort_order=True, edges=False, edges_width=0.1, edges_color='grey', neighbors_key=None, arrows=False, arrows_kwds=None, groups=None, components=None, layer=None, projection='2d', scale_factor=None, color_map=None, cmap=None, palette=None, na_color=None, na_in_legend=True, size=1.0, frameon=None, legend_fontsize=None, legend_fontweight='bold', legend_loc='right margin', legend_fontoutline=None, vmax=None, vmin=None, vcenter=None, norm=None, add_outline=False, outline_width=(0.3, 0.05), outline_color=('black', 'white'), ncols=4, hspace=0.25, wspace=None, title=None, show=None, save=None, ax=None, return_fig=None, basis='spatial', img=None, img_key=<Empty.token: 0>, library_id=<Empty.token: 0>, crop_coord=None, alpha_img=1.0, bw=False, spot_size=None, **kwargs)
Scatter plot in spatial coordinates.
This function allows overlaying data on top of images. Use the parameter
img_key
to see the image in the background And the parameterlibrary_id
to select the image. By default,'hires'
and'lowres'
are attempted.Use
crop_coord
,alpha_img
, andbw
to control how it is displayed. Usesize
to scale the size of the Visium spots plotted on top.As this function is designed to for imaging data, there are two key assumptions about how coordinates are handled:
1. The origin (e.g
(0, 0)
) is at the top left – as is common convention with image data.2. Coordinates are in the pixel space of the source image, so an equal aspect ratio is assumed.
If your anndata object has a
"spatial"
entry in.uns
, theimg_key
andlibrary_id
parameters to find values forimg
,scale_factor
, andspot_size
arguments. Alternatively, these values be passed directly.- Parameters
- adata :
AnnData
AnnData
Annotated data matrix.
- color :
str
|Sequence
[str
] |None
Union
[str
,Sequence
[str
],None
] (default:None
) Keys for annotations of observations/cells or variables/genes, e.g.,
'ann1'
or['ann1', 'ann2']
.- gene_symbols :
str
|None
Optional
[str
] (default:None
) Column name in
.var
DataFrame that stores gene symbols. By defaultvar_names
refer to the index column of the.var
DataFrame. Setting this option allows alternative names to be used.- use_raw :
bool
|None
Optional
[bool
] (default:None
) Use
.raw
attribute ofadata
for coloring with gene expression. IfNone
, defaults toTrue
iflayer
isn’t provided andadata.raw
is present.- layer :
str
|None
Optional
[str
] (default:None
) Name of the AnnData object layer that wants to be plotted. By default adata.raw.X is plotted. If
use_raw=False
is set, thenadata.X
is plotted. Iflayer
is set to a valid layer name, then the layer is plotted.layer
takes precedence overuse_raw
.- library_id :
str
|Empty
Union
[str
,Empty
] (default:<Empty.token: 0>
) library_id for Visium data, e.g. key in
adata.uns["spatial"]
.- img_key :
str
|None
|Empty
Union
[str
,None
,Empty
] (default:<Empty.token: 0>
) Key for image data, used to get
img
andscale_factor
from"images"
and"scalefactors"
entires for this library. To use spatial coordinates, but not plot an image, passimg_key=None
.- img :
ndarray
|None
Optional
[ndarray
] (default:None
) image data to plot, overrides
img_key
.- scale_factor :
float
|None
Optional
[float
] (default:None
) Scaling factor used to map from coordinate space to pixel space. Found by default if
library_id
andimg_key
can be resolved. Otherwise defaults to1.
.- spot_size :
float
|None
Optional
[float
] (default:None
) Diameter of spot (in coordinate space) for each point. Diameter in pixels of the spots will be
size * spot_size * scale_factor
. This argument is required if it cannot be resolved from library info.- crop_coord :
Tuple
[int
,int
,int
,int
] |None
Optional
[Tuple
[int
,int
,int
,int
]] (default:None
) Coordinates to use for cropping the image (left, right, top, bottom). These coordinates are expected to be in pixel space (same as
basis
) and will be transformed byscale_factor
. If not provided, image is automatically cropped to bounds ofbasis
, plus a border.- alpha_img :
float
float
(default:1.0
) Alpha value for image.
- bw :
bool
|None
Optional
[bool
] (default:False
) Plot image data in gray scale.
- sort_order :
bool
bool
(default:True
) For continuous annotations used as color parameter, plot data points with higher values on top of others.
- groups :
str
|None
Optional
[str
] (default:None
) Restrict to a few categories in categorical observation annotation. The default is not to restrict to any groups.
- components :
str
|Sequence
[str
]Union
[str
,Sequence
[str
]] (default:None
) For instance,
['1,2', '2,3']
. To plot all available components usecomponents='all'
.- projection : {‘2d’, ‘3d’}
Literal
[‘2d’, ‘3d’] (default:'2d'
) Projection of plot (default:
'2d'
).- legend_loc :
str
str
(default:'right margin'
) Location of legend, either
'on data'
,'right margin'
or a valid keyword for theloc
parameter ofLegend
.- legend_fontsize :
int
|float
| {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} |None
Union
[int
,float
,Literal
[‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’],None
] (default:None
) Numeric size in pt or string describing the size. See
set_fontsize()
.- legend_fontweight :
int
| {‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’}Union
[int
,Literal
[‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’]] (default:'bold'
) Legend font weight. A numeric value in range 0-1000 or a string. Defaults to
'bold'
iflegend_loc == 'on data'
, otherwise to'normal'
. Seeset_fontweight()
.- legend_fontoutline :
int
|None
Optional
[int
] (default:None
) Line width of the legend font outline in pt. Draws a white outline using the path effect
withStroke
.- size :
float
float
(default:1.0
) Point size. If
None
, is automatically computed as 120000 / n_cells. Can be a sequence containing the size for each cell. The order should be the same as in adata.obs.- color_map :
Colormap
|str
|None
Union
[Colormap
,str
,None
] (default:None
) Color map to use for continous variables. Can be a name or a
Colormap
instance (e.g."magma
”,"viridis"
ormpl.cm.cividis
), seeget_cmap()
. IfNone
, the value ofmpl.rcParams["image.cmap"]
is used. The defaultcolor_map
can be set usingset_figure_params()
.- palette :
str
|Sequence
[str
] |Cycler
|None
Union
[str
,Sequence
[str
],Cycler
,None
] (default:None
) Colors to use for plotting categorical annotation groups. The palette can be a valid
ListedColormap
name ('Set2'
,'tab20'
, …), aCycler
object, a dict mapping categories to colors, or a sequence of colors. Colors must be valid to matplotlib. (seeis_color_like()
). IfNone
,mpl.rcParams["axes.prop_cycle"]
is used unless the categorical variable already has colors stored inadata.uns["{var}_colors"]
. If provided, values ofadata.uns["{var}_colors"]
will be set.- na_color :
str
|Tuple
[float
, …] |None
Union
[str
,Tuple
[float
, …],None
] (default:None
) Color to use for null or masked values. Can be anything matplotlib accepts as a color. Used for all points if
color=None
.- na_in_legend :
bool
bool
(default:True
) If there are missing values, whether they get an entry in the legend. Currently only implemented for categorical legends.
- frameon :
bool
|None
Optional
[bool
] (default:None
) Draw a frame around the scatter plot. Defaults to value set in
set_figure_params()
, defaults toTrue
.- title :
str
|Sequence
[str
] |None
Union
[str
,Sequence
[str
],None
] (default:None
) Provide title for panels either as string or list of strings, e.g.
['title1', 'title2', ...]
.- vmin :
str
|float
| (Sequence
[float
]) →float
|Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]] |None
Union
[str
,float
,Callable
[[Sequence
[float
]],float
],Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]],None
] (default:None
) The value representing the lower limit of the color scale. Values smaller than vmin are plotted with the same color as vmin. vmin can be a number, a string, a function or
None
. If vmin is a string and has the formatpN
, this is interpreted as a vmin=percentile(N). For example vmin=’p1.5’ is interpreted as the 1.5 percentile. If vmin is function, then vmin is interpreted as the return value of the function over the list of values to plot. For example to set vmin tp the mean of the values to plot,def my_vmin(values): return np.mean(values)
and then setvmin=my_vmin
. If vmin is None (default) an automatic minimum value is used as defined by matplotlibscatter
function. When making multiple plots, vmin can be a list of values, one for each plot. For examplevmin=[0.1, 'p1', None, my_vmin]
- vmax :
str
|float
| (Sequence
[float
]) →float
|Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]] |None
Union
[str
,float
,Callable
[[Sequence
[float
]],float
],Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]],None
] (default:None
) The value representing the upper limit of the color scale. The format is the same as for
vmin
.- vcenter :
str
|float
| (Sequence
[float
]) →float
|Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]] |None
Union
[str
,float
,Callable
[[Sequence
[float
]],float
],Sequence
[Union
[str
,float
,Callable
[[Sequence
[float
]],float
]]],None
] (default:None
) The value representing the center of the color scale. Useful for diverging colormaps. The format is the same as for
vmin
. Example: sc.pl.umap(adata, color=’TREM2’, vcenter=’p50’, cmap=’RdBu_r’)- add_outline :
bool
|None
Optional
[bool
] (default:False
) If set to True, this will add a thin border around groups of dots. In some situations this can enhance the aesthetics of the resulting image
- outline_color :
Tuple
[str
,str
]Tuple
[str
,str
] (default:('black', 'white')
) Tuple with two valid color names used to adjust the add_outline. The first color is the border color (default: black), while the second color is a gap color between the border color and the scatter dot (default: white).
- outline_width :
Tuple
[float
,float
]Tuple
[float
,float
] (default:(0.3, 0.05)
) Tuple with two width numbers used to adjust the outline. The first value is the width of the border color as a fraction of the scatter dot size (default: 0.3). The second value is width of the gap color (default: 0.05).
- ncols :
int
int
(default:4
) Number of panels per row.
- wspace :
float
|None
Optional
[float
] (default:None
) Adjust the width of the space between multiple panels.
- hspace :
float
float
(default:0.25
) Adjust the height of the space between multiple panels.
- return_fig :
bool
|None
Optional
[bool
] (default:None
) Return the matplotlib figure.
- kwargs
Arguments to pass to
matplotlib.pyplot.scatter()
, for instance: the maximum and minimum values (e.g.vmin=-2, vmax=5
).- show :
bool
|None
Optional
[bool
] (default:None
) Show the plot, do not return axis.
- save :
str
|bool
|None
Union
[str
,bool
,None
] (default:None
) If
True
or astr
, save the figure. A string is appended to the default filename. Infer the filetype if ending on {'.pdf'
,'.png'
,'.svg'
}.- ax :
Axes
|None
Optional
[Axes
] (default:None
) A matplotlib axes object. Only works if plotting a single component.
- adata :
- Return type
- Returns
If
show==False
aAxes
or a list of it.
Examples
This function behaves very similarly to other embedding plots like
umap()
>>> adata = sc.datasets.visium_sge("Targeted_Visium_Human_Glioblastoma_Pan_Cancer") >>> sc.pp.calculate_qc_metrics(adata, inplace=True) >>> sc.pl.spatial(adata, color="log1p_n_genes_by_counts")
See also
scanpy.datasets.visium_sge()
Example visium data.
- → tutorial: spatial/basic-analysis
Tutorial on spatial analysis.