【问题标题】:How to get colors from dendrogram in MATLAB?如何从 MATLAB 中的树状图中获取颜色?
【发布时间】:2021-07-09 14:28:21
【问题描述】:

我已经生成了一些示例数据的树状图,我想为我的观察结果分配颜色。

下面是如何将颜色从树状图匹配到观察结果的示例。

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 with the 'Color' property

【问题讨论】:

    标签: matlab colors dendrogram


    【解决方案1】:

    您需要在代码中添加以下内容:

    %% 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
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2011-12-13
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 2014-10-14
      相关资源
      最近更新 更多