How can I color my dendrogram plot such that the colors correspond to clusters generated by the "cluster" function in the Statistics and Machine Learning Toolbox?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 27 de Jun. de 2009
Editada: MathWorks Support Team
el 15 de Jul. de 2024
I generate a dendrogram plot by running the following code at the MATLAB prompt:
load fisheriris.mat
numClusters = 3;
dist = pdist(meas, "euclidean");
link = linkage(dist, "average");
clust = cluster(link, "maxclust", numClusters);
dendrogram(link)
I would like different sections of the dendrogram plot colored such that they correspond to the clusters returned by the "cluster" function. How can I accomplish this?
Respuesta aceptada
MathWorks Support Team
el 15 de Jul. de 2024
Editada: MathWorks Support Team
el 15 de Jul. de 2024
As of MATLAB R2024b:
You can use the "ClusterIndices" name-value pair of the "dendrogram" function to specify cluster assignments for your dendrogram plot. For example:
load fisheriris.mat
numClusters = 3;
dist = pdist(meas, "euclidean");
link = linkage(dist, "average");
clust = cluster(link, "maxclust", numClusters);
dendrogram(link, "ClusterIndices", clust)
In MATLAB R2024a and earlier:
You can use the "ColorThreshold" name-value pair of the "dendrogram" function as a workaround, as follows:
load fisheriris.mat
numClusters = 3;
dist = pdist(meas, "euclidean");
link = linkage(dist, "average");
clust = cluster(link, "maxclust", numClusters);
color = link(end-numClusters+2, 3) - eps;
dendrogram(link, "ColorThreshold", color)
The above code will work for any values of "NumCluster" set to 2 or higher. The idea is to use the distance information returned by the "linkage" function to identify a distance cut-off point such that coloring the clusters on the dendrogram plot below that point will result in the desired coloring effect. Since the distance information is returned in the third column of the "link" variable in ascending order, you can see that the value of "color" is set just below the line that would break the dendrogram plot into "NumClusters" clusters.
NOTE: The above code might not work well in situations with many repeated distance values returned in the "link" variable. This code is only provided as a guideline, and you should modify it as necessary to fit a given problem.
1 comentario
Cam Salzberger
el 15 de Abr. de 2016
Hello Denise,
That's a good suggestion. I do not believe that there is currently an easy way to do this. I have submitted an enhancement request for this functionality, so we may see it in a future release of MATLAB.
You can check the 'Color' property of the lines in the first output of "dendrogram". This would at least give you which color options there are. The lines appear to have been drawn from top-down on the plot, so the last entry in "H" is the top-most line-segment. If you can organize the clusters by which branches off first, you may be able to work out which lines correspond to which nodes. It's a tricky proposition though.
-Cam
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!