Dendrogram with custom colouring

28 次查看(过去 30 天)
Hi, I am preparing dendrograms in MATLAB. I can set the colour threshold as follows:
>> figure()
H = dendrogram(tree,0,'ColorThreshold',1);
set(H,'LineWidth',1.5)
this gives the following output:
I have two questions regarding the above:
1) How can I have MATLAB only colour the largest cluster below my threshold (this would be the one in red)
2) How can I specify the colour
Thanks in advance.

采纳的回答

Adam
Adam 2017-2-14
If you use the
hLines = dendrogram(...)
syntax you will get an array of handles as output. These will be handles to the lines created. These can be coloured individually and with any colour you want. I assume the lines are ordered as specified by the leafOrder although I haven't checked.
X = rand(10,3);
tree = linkage(X,'average');
D = pdist(X);
leafOrder = optimalleaforder(tree,D);
figure()
h = dendrogram(tree,'Reorder',leafOrder);
h(1).Color = 'r';
h(8).Color = [0 0.5 0];
  3 个评论
Adam
Adam 2017-2-14
编辑:Adam 2017-2-14
Which version of Matlab are you using? If you are pre R2014b you will instead need
set( h(1), 'Color', 'r' )
etc.
Using this syntax you can also set multiple at the same time which you can't with the newer syntax e.g.
set( h( [1 3 7] ), 'Color', 'r' );

请先登录,再进行评论。

更多回答(1 个)

micholeodon
micholeodon 2021-4-14
编辑:micholeodon 2021-4-14
Here is how you get colors from your dendrogram and link it with the observations.
By changing 'Color' property in lines stored in h variable below you can change colors in your dendrogram.
clear; close all; clc;
%% Generate example data
rng('default') % For reproducibility
N = 10; % number of observations
X = rand(N,3);
%% Get linkage
tree = linkage(X, 'average');
%% Get desired number of clusters
nClusters = 2;
cutoff = median([tree(end-nClusters+1,3) tree(end-nClusters+2, 3)]);
%% plot tree
figure
h = dendrogram(tree, 'ColorThreshold', cutoff); % h contains Line objects
%% get colors
linesColor = cell2mat(get(h,'Color')); % get lines color;
colorList = unique(linesColor, 'rows');
% NOTE that each row is single line corresponding to the same row in tree
% variable. I use that property below.
X_color = zeros(N,3);
X_cluster = zeros(N,1);
for iLeaf = 1:N
[iRow, ~] = find(tree==iLeaf);
color = linesColor(iRow,:); % !
% assign color to each observation
X_color(iLeaf,:) = color;
% assign cluster number to each observation
X_cluster(iLeaf,:) = find(ismember(colorList, color, 'rows'));
end
%% changing lines color
h(5).Color = [0 1 0]
  2 个评论
Niraj Desai
Niraj Desai 2023-3-2
Thank you! I've been going in circles all afternoon trying to figure this out. Much obliged.
T0m07
T0m07 2023-5-22
编辑:T0m07 2023-5-22
I don't think this works if you have more than 30 data points, since then you have more data points than leaves, and your code errors during the for loop.
For your code to work, you need to provide N as an additional input to dendrogram() to define the Maximum number of leaf nodes.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by