# pass distance matrix to seaborn clustermap

• Last Update :
• Techknowledgy :

Suppose `distMatrix` is your matrix of distances (don't have to be Euclidean), with entry in row `i` and column `j` representing the distance between the `i`th and `j`th objects. Then:

```#
import packages
from scipy.cluster
import hierarchy
import scipy.spatial.distance as ssd
import seaborn as sns

distArray = ssd.squareform(distMatrix)

# make clustermap

Suggestion : 2

Plot a matrix dataset as a hierarchically-clustered heatmap.,Distance metric to use for the data. See scipy.spatial.distance.pdist() documentation for more options. To use different metrics (or methods) for rows and columns, you may construct each linkage matrix yourself and provide them as {row,col}_linkage.,Linkage method to use for calculating clusters. See scipy.cluster.hierarchy.linkage() documentation for more information.,Precomputed linkage matrix for the rows or columns. See scipy.cluster.hierarchy.linkage() for specific formats.

```>>>
import seaborn as sns;
sns.set_theme(color_codes = True) >>>
species = iris.pop("species") >>>
g = sns.clustermap(iris)```
```>>> g = sns.clustermap(iris,
...figsize = (7, 5),
...row_cluster = False,
...dendrogram_ratio = (.1, .2),
...cbar_pos = (0, .2, .03, .4))```
```>>> lut = dict(zip(species.unique(), "rbg")) >>>
row_colors = species.map(lut) >>>
g = sns.clustermap(iris, row_colors = row_colors)```
`>>> g = sns.clustermap(iris, cmap = "mako", vmin = 0, vmax = 10)`
`>>> g = sns.clustermap(iris, metric = "correlation")`
`>>> g = sns.clustermap(iris, method = "single")`

Suggestion : 3

I want to pass my own distance matrix (row anycodings_seaborn linkages) to seaborn clustermap.,Is there an option where it purely uses my anycodings_seaborn distances and creates the linkages?,My distance matrix is already based on a anycodings_seaborn certain metric and method, why would I want anycodings_seaborn to recalculate this in scipy hierarchy anycodings_seaborn linkage ?,Use Distance Matrix in anycodings_seaborn scipy.cluster.hierarchy.linkage()?

Suppose distMatrix is your matrix of anycodings_hierarchical-clustering distances (don't have to be Euclidean), anycodings_hierarchical-clustering with entry in row i and column j anycodings_hierarchical-clustering representing the distance between the anycodings_hierarchical-clustering ith and jth objects. Then:

```#
import packages
from scipy.cluster
import hierarchy
import scipy.spatial.distance as ssd
import seaborn as sns

distArray = ssd.squareform(distMatrix)

`dm = pdist(X, lambda u, v: np.sqrt(((u - v) ** 2).sum()))`
`dm = pdist(X, sokalsneath)`
`dm = pdist(X, 'sokalsneath')`