Dendrogram with custom colouring

31 ビュー (過去 30 日間)
KA
KA 2017 年 2 月 14 日
編集済み: T0m07 2023 年 5 月 22 日
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' );
KA
KA 2017 年 2 月 14 日
Sorted - thank you.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeImages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by