Skip to content

densityPlot2D

densityPlot2D

densityPlot2D(
    data,
    markerA,
    markerB=None,
    layer=None,
    subset=None,
    imageId="imageid",
    ncols=None,
    cmap="jet",
    figsize=(3, 3),
    hline="auto",
    vline="auto",
    fontSize=None,
    dpi=100,
    xticks=None,
    yticks=None,
    outputDir=None,
    fileName="densityPlot2D.pdf",
    show=True,
    returnData=False,
    returnFig=False,
    transparent=False,
    sdataTable=None,
    **kwargs
)

Two-marker density plots, one panel per marker pair.

Pairs are formed by zipping markerA with markerB. Passing a single list to markerA and leaving markerB as None pairs each marker with the next one, which is a quick way to sweep a panel.

Parameters:

Name Type Description Default
data (AnnData | SpatialData | str, required)

The cell table. An AnnData, a path to an .h5ad file, a SpatialData object, or a path to a .zarr SpatialData store.

required
markerA (str | list, required)

Marker(s) on the x axis.

required
markerB str | list

Marker(s) on the y axis. When None, markerA[1:] is used, pairing each marker with its successor.

None
layer str

Which matrix to plot. None uses .X, 'raw' uses .raw.X, any other string names a key in .layers.

None
subset str | list

Restrict to these values of imageId.

None
imageId str

Column in obs holding image identifiers, used by subset.

'imageid'
ncols int

Panels per row. Defaults to min(3, number of pairs).

None
cmap str

Matplotlib colormap for the density.

'jet'
figsize tuple

Size of a single panel in inches.

(3, 3)
hline float

Horizontal reference line. 'auto' (the default) draws none.

'auto'
vline float

Vertical reference line. 'auto' (the default) draws none.

'auto'
fontSize int

Axis label font size.

None
xticks list

Explicit x tick positions.

None
yticks list

Explicit y tick positions.

None
**kwargs dict

Passed through to Axes.hist2d. bins defaults to 100.

{}
outputDir str

Directory to save the figure in. When None nothing is written.

None
fileName str

File name for the saved figure. The extension decides the format.

'densityPlot2D.pdf'
show bool

Call plt.show() before returning. Set False in scripts and notebooks that save rather than display.

True
returnData bool

Return the DataFrame behind the plot instead of drawing it.

False
returnFig bool

Return (fig, axes). With returnData as well, returns (fig, axes, plotData).

False
dpi int

Resolution of the saved figure.

100
transparent bool

Save with a transparent background.

False
sdataTable str

Which SpatialData table to read. Ignored for AnnData input, and optional when the store has exactly one table.

None

Returns:

Name Type Description
result None | DataFrame | tuple

None by default; the cell x marker DataFrame with returnData=True; (fig, axes) with returnFig=True.

Example
# One biaxial plot with gate lines at the rescaled boundary.
sp.pl.densityPlot2D(
    adata, markerA="CD45", markerB="ECAD", layer="rescaled",
    hline=0.5, vline=0.5,
)

# Several pairs at once.
sp.pl.densityPlot2D(
    adata,
    markerA=["CD45", "CD3", "CD8"],
    markerB=["ECAD", "CD4", "FOXP3"],
    outputDir="figures",
    show=False,
)