1
$\begingroup$

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?

$\endgroup$
0
5
$\begingroup$

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

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/HeatmapPlot.m"]

Make a random symmetric matrix:

SeedRandom[23];
{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:

Grid[Table[
  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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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