scanpy.tl.dendrogram#
- scanpy.tl.dendrogram(adata, groupby, *, n_pcs=None, use_rep=None, var_names=None, use_raw=None, cor_method='pearson', linkage_method='complete', optimal_ordering=False, key_added=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
- n_pcs
int|None(default:None) Use this many PCs. If
n_pcs==0use.Xifuse_rep is None.- use_rep
str|None(default:None) Use the indicated representation.
'X'or any key for.obsmis valid. IfNone, the representation is chosen automatically: For.n_vars<N_PCS(default: 50),.Xis used, otherwise ‘X_pca’ is used. If ‘X_pca’ is not present, it’s computed with default parameters orn_pcsif present.- var_names
Sequence[str] |None(default:None) List of var_names to use for computing the hierarchical clustering. If
var_namesis given, thenuse_repandn_pcsare ignored.- use_raw
bool|None(default:None) 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.- key_added
str|None(default:None) By default, the dendrogram information is added to
.uns[f'dendrogram_{groupby}']. Notice that thegroupbyinformation is added to the dendrogram.- inplace
bool(default:True) If
True, adds dendrogram information toadata.uns[key_added], else this function returns the information.
- adata
- Return type:
- Returns:
Returns
Noneifinplace=True, else returns adictwith dendrogram information. Sets the following field ifinplace=True:adata.uns[f'dendrogram_{group_by}' | 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)