scanpy.tl.dendrogram#
- scanpy.tl.dendrogram(adata, groupby, *, n_pcs=None, use=None, var_names=None, use_raw=None, cor_method='pearson', linkage_method='complete', optimal_ordering=False, out=False, key_added=None, use_rep=None, inplace=True)[source]#
Compute a hierarchical clustering for the given
groupbycategories.By default, the PCA representation is used unless
.Xhas less than 50 variables.Alternatively, a list of
var_names(e.g. genes) can be given.Average values of either
var_namesor components are used to compute a correlation matrix.The hierarchical clustering can be visualized using
scanpy.pl.dendrogram()or multiple other visualizations that can include a dendrogram:matrixplot(),heatmap(),dotplot(), andstacked_violin().Note
The computation of the hierarchical clustering is based on predefined groups and not per cell. The correlation matrix is computed using by default pearson but other methods are available.
Array type support# Array type
supported
… experimentally in dask
Array✅
❌
✅
❌
- Parameters:
- adata
AnnData Annotated data matrix
- groupby
AdRef|str|Sequence[AdRef|str] The observations grouping(s) to cluster. Given by references like
A.obs['leiden'], or by column names (strs areresolve()d withScanpyV2Preview).- n_pcs
int|None(default:None) Use this many PCs. If
n_pcs==0use.Xifuse is None.- use
LayerAcc|MultiAcc|str|None(default:None) Use the indicated representation: a
LayerAcc(e.g.A.X,A.layers[...]) orMultiAcc(e.g.A.obsm[...]).strs areresolve()d, e.g.'obsm.pca'.If
None, the representation is chosen automatically: For.n_vars<N_PCS(default: 50),.Xis used, otherwise the PCA representation (.obsm['X_pca'], or.obsm['pca']if it was computed underScanpyV2Preview). If it is not present, it’s computed with default parameters orn_pcsif present.- use_rep
str|None(default:None) Deprecated since version 1.13.0: Use
useinstead.'X'or a key ofobsm, i.e.use_rep='k'isuse=A.obsm['k']anduse_rep='X'isuse=A.X.- var_names
Sequence[str] |None(default:None) List of var_names to use for computing the hierarchical clustering. If
var_namesis given, thenuseandn_pcsare ignored.- use_raw
bool|None(default:None) Deprecated since version 1.13.0:
rawis deprecated. Keep the data in a layer instead.Only when
var_namesis not None. Userawattribute ofadataif present.- cor_method
str(default:'pearson') Correlation method to use. Options are ‘pearson’, ‘kendall’, and ‘spearman’
- linkage_method
str(default:'complete') Linkage method to use. See
scipy.cluster.hierarchy.linkage()for more information.- optimal_ordering
bool(default:False) Same as the optimal_ordering argument of
scipy.cluster.hierarchy.linkage()which reorders the linkage matrix so that the distance between successive leaves is minimal.- out
bool(default:False) Where to write the dendrogram information. If
True, the dendrogram information is returned instead of written. IfFalse, it is written to.uns[f'dendrogram_{groupby}'](withgroupbyjoined by_).- key_added
str|None(default:None) Name of the
unsentry to write to instead (until there are.unsaccessors).- inplace
bool(default:True) Deprecated since version 1.13.0: Use
out=Trueinstead ofinplace=False.inplace=Falseisout=True.
- adata
- Return type:
- Returns:
Returns a
dictwith dendrogram information ifout=True, elseNone. Sets the following field:adata.uns[f'dendrogram_{groupby}' | key_added]dictDendrogram information.
Examples
>>> import scanpy as sc >>> adata = sc.datasets.pbmc68k_reduced() >>> sc.tl.dendrogram(adata, groupby="bulk_labels") >>> sc.pl.dendrogram(adata, groupby="bulk_labels") <Axes: > >>> markers = ["C1QA", "PSAP", "CD79A", "CD79B", "CST3", "LYZ"] >>> sc.pl.dotplot(adata, markers, groupby="bulk_labels", dendrogram=True)