如何更改树形图中的簇颜色

时间:2016-01-22 13:59:56

标签: matlab plot cluster-analysis hierarchical-clustering dendrogram

如何在MATLAB树形图中更改分配给每个簇的颜色以匹配我自己的自定义颜色方案?广泛的谷歌搜索还没有产生解决方案。

1 个答案:

答案 0 :(得分:0)

%%// Hierarchical clustering
T = linkage(data,'average','spearman');
D = pdist(data, 'spearman');
leafOrder = optimalleaforder(T, D);
th = 0.726;
H = dendrogram(T, 0,'ReOrder', leafOrder, 'Orientation', 'left', 'ColorThreshold', th);
h = gca;
set(h, 'YTickLabel', labels(leafOrder));

%// Changing the colours
lineColours = cell2mat(get(H,'Color'));
colourList = unique(lineColours, 'rows');

myColours = [230,186,0;
             127,127,127;
             10,35,140;
             176,0,0;
             158,182,72;
             79,129,189;
             209,99,9;
             4,122,146]/255;

%// Replace each colour (colour by colour). Start from 2 because the first colour are the "unclustered" black lines             
for colour = 2:size(colourList,1)
    %// Find which lines match this colour
    idx = ismember(lineColours, colourList(colour,:), 'rows');
    %// Replace the colour for those lines
    lineColours(idx, :) = repmat(myColours(colour-1,:),sum(idx),1);
end
%// Apply the new colours to the chart's line objects (line by line)
for line = 1:size(H,1)
    set(H(line), 'Color', lineColours(line,:));
end