# Dendrogram with custom colouring

60 views (last 30 days)
KA on 14 Feb 2017
Edited: T0m07 on 22 May 2023
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

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);
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 CommentsShow 1 older commentHide 1 older comment
Edited: Adam on 14 Feb 2017
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 on 14 Feb 2017
Sorted - thank you.

micholeodon on 14 Apr 2021
Edited: micholeodon on 14 Apr 2021
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 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]
Niraj Desai on 2 Mar 2023
Thank you! I've been going in circles all afternoon trying to figure this out. Much obliged.
T0m07 on 22 May 2023
Edited: T0m07 on 22 May 2023
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.

### Categories

Find more on Hierarchical Clustering in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!