Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 38 ยท Regression and Matrices

Clustermaps: Hierarchically Clustered Heatmaps

Group similar rows and columns automatically with sns.clustermap: linkage methods, distance metrics, standardizing, color bars and reading dendrograms.

A plain heatmap shows rows and columns in the order you gave them. If that order is arbitrary (alphabetical, say), patterns hide in plain sight. A clustermap reorders both axes so that similar rows sit next to each other and similar columns sit next to each other, and it draws tree diagrams (dendrograms) along the edges that show how the groups were formed.

sns.clustermap() does the clustering with SciPy's hierarchical algorithms and then draws the heatmap. This lesson teaches how it works, which options matter, and how to read the result.

How hierarchical clustering works

Hierarchical clustering in four steps

Measure distance How far apart are two rows?
Merge closest Join the two nearest items.
Repeat Treat each merge as a new item.
Reorder Leaves of the tree set the order.

Short branches mean similar items.

1 / 2

Every row starts as its own cluster. The algorithm repeatedly merges the two closest clusters until one remains. The merge history is a tree; the order of the leaves becomes the new row order. Two choices shape the result: the metric (how to measure distance between two rows, for example "euclidean" or "correlation") and the method (how to measure distance between two clusters, for example "average", "complete", "single" or "ward").

Your first clustermap

The data below hides three groups of customers: some buy mostly tech, some mostly food and some mostly clothing. The rows are shuffled, so the structure is invisible in a plain heatmap. Watch it appear:

Python Plain heatmap versus clustermap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

clustermap() is figure-level and returns a ClusterGrid. Its main parts are g.ax_heatmap, g.ax_row_dendrogram, g.ax_col_dendrogram and g.cax (the colour bar). Use g.figure for the whole figure, which is why suptitle goes there.

Standardising: z_score and standard_scale

Clustering uses distances, so columns on large scales dominate. If one column is in dollars (thousands) and another is a rating (1 to 5), the dollar column decides everything. Normalise first:

  • standard_scale=0 rescales each row to 0-1, standard_scale=1 rescales each column.
  • z_score=0 or 1 converts rows or columns to z-scores (mean 0, standard deviation 1), which pairs well with a diverging colormap and center=0.
Python Why scaling matters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the heatmap shows the standardised values, the colour bar then reads "standard deviations from the column mean", which is easier to interpret than raw units.

Methods and metrics

Parameter Common values Notes
method "average" (default), "complete", "single", "ward", "centroid" "ward" makes compact, equal-sized clusters and requires Euclidean distance. "single" chains clusters together.
metric "euclidean" (default), "cityblock", "correlation", "cosine" "correlation" clusters by shape (up and down together), not by level.

Cluster only one axis with row_cluster=False or col_cluster=False, which keeps your own order on that axis. This is handy for time columns, which have a natural order.

Colour strips and a correlation clustermap

row_colors and col_colors add a thin strip of colour beside the heatmap, mapping each row to a category, so you can check whether the clusters match a known label. A classic use is a clustered correlation matrix, which reorders variables so that correlated ones sit together:

Python Clustered correlation matrix with row colours

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dendrogram tells you the same story as the colours: body measurements join first, money variables join first, and luck branches off alone.

Practical tips

  • Missing values break clustering. Fill or drop them first: data.dropna().
  • Clustering thousands of rows is slow and the tree is unreadable; filter to the most variable rows first.
  • The dendrogram shows similarity, not importance; a different method can reorder branches.
  • g.dendrogram_row.reordered_ind gives the new row order as integer positions, useful for exporting the grouped table.
  • Use dendrogram_ratio=(0.1, 0.1) to shrink the trees, and cbar_pos=None to hide the colour bar.

Back to the simpler picture in heatmaps, or on to arranging many plots with facet grids.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a clustermap do that a heatmap does not?

    clustermap runs hierarchical clustering and reorders both axes, adding trees to show the merges.
  2. 2. Columns have very different scales. What should you do before clustering?

    Distances are dominated by large-scale columns unless you standardise them.
  3. 3. What does the metric argument control?

    metric measures distance between observations; method decides how clusters are linked.
  4. 4. How do you keep your own order on the columns?

    col_cluster=False skips clustering for the columns.

Summary

  • clustermap() clusters rows and columns hierarchically and draws the dendrograms with the heatmap.
  • metric measures row distance, method links clusters; "ward" needs Euclidean distance.
  • Standardise with z_score or standard_scale when column scales differ.
  • row_colors, col_colors, row_cluster and col_cluster add labels and control ordering.

Next: Facet Grids