I have a similarity matrix and when I plot it as a heatmap, it looks like:

enter image description here

I have tried using 'Agglomerate' to cluster the underlying matrix but I cannot figure out how to get the clusters to show in a new heatmap. How can I do this?


1 Answer 1


You can use the package "HeatmapPlot.m". Below are given examples.


Make a random symmetric matrix:

{m, n} = {80, 50};
mat = RandomReal[{0, 100}, {m, n}];
mat = SparseArray[RandomSample[Most@ArrayRules@SparseArray[mat], Floor[4*m]]];
mat = mat.Transpose[mat];
MatrixPlot[mat, MaxPlotPoints -> 300]

enter image description here

Heatmap plot with clustering for the random symmetric matrix:

HeatmapPlot[mat, Dendrogram -> True, ImageSize -> Large]

enter image description here

You can specify distance function and linkage:

  HeatmapPlot[ mat, 
   Dendrogram -> True, 
   DistanceFunction -> dist,
   HierarchicalClustering`Linkage -> link, 
   PlotLabel -> {dist, link}, ImageSize -> Medium],
  {dist, {EuclideanDistance, CosineDistance}},
  {link, {"Single", "Complete", "Ward"}}], 
  Dividers -> All, FrameStyle -> GrayLevel[0.8]]

enter image description here

  • $\begingroup$ Maybe I don't understand something fundamentally, but the matrix is symmetric along one diagonal, but the dendrograms seem to set the symmetry along the other diagonal. Please, explain. $\endgroup$ Commented Oct 14, 2022 at 14:12
  • $\begingroup$ The dendrograms are derived by the column- and row-similarities. They might "imply" order that is not immediately obvious from the matrix plot. Also, if the matrix plots are made with larger number of points the impressions they give might be different. $\endgroup$ Commented Oct 14, 2022 at 18:27
  • $\begingroup$ Yes, ok.. Still. Take a look here researchgate.net/publication/… It seems that dendrogram at the right should be flipped along horizontal axis. $\endgroup$ Commented Oct 17, 2022 at 14:13
  • $\begingroup$ @ФилиппЦветков Thanks, I will read the article later this week... $\endgroup$ Commented Oct 17, 2022 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.