I have a similarity matrix and when I plot it as a heatmap, it looks like:
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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou 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]
Heatmap plot with clustering for the random symmetric matrix:
HeatmapPlot[mat, Dendrogram -> True, ImageSize -> Large]
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]]