Show rows on clustered kmeans data

烂漫一生 提交于 2019-12-06 07:14:59

You could use the data cursors feature which displays a tooltip when you select a point from the plot. You can use a modified update function to display all sorts of information about the point selected.

Here is a working example:

function customCusrorModeDemo()
    %# data
    D = load('fisheriris');
    data = D.meas;
    [clustIdx,labels] = grp2idx(D.species);
    K = numel(labels);
    clr = hsv(K);

    %# instance indices grouped according to class
    ind = accumarray(clustIdx, 1:size(data,1), [K 1], @(x){x});

    %# plot
    %#gscatter(data(:,1), data(:,2), clustIdx, clr)
    hLine = zeros(K,1);
    for k=1:K
        hLine(k) = line(data(ind{k},1), data(ind{k},2), data(ind{k},3), ...
            'LineStyle','none', 'Color',clr(k,:), ...
            'Marker','.', 'MarkerSize',15);
    end
    xlabel('SL'), ylabel('SW'), zlabel('PL')
    legend(hLine, labels)
    view(3), box on, grid on

    %# data cursor
    hDCM = datacursormode(gcf);
    set(hDCM, 'UpdateFcn',@updateFcn, 'DisplayStyle','window')
    set(hDCM, 'Enable','on')

    %# callback function
    function txt = updateFcn(~,evt)
        hObj = get(evt,'Target');   %# line object handle
        idx = get(evt,'DataIndex'); %# index of nearest point

        %# class index of data point
        cIdx = find(hLine==hObj, 1, 'first');

        %# instance index (index into the entire data matrix)
        idx = ind{cIdx}(idx);

        %# output text
        txt = {
            sprintf('SL: %g', data(idx,1)) ;
            sprintf('SW: %g', data(idx,2)) ;
            sprintf('PL: %g', data(idx,3)) ;
            sprintf('PW: %g', data(idx,4)) ;
            sprintf('Index: %d', idx) ;
            sprintf('Class: %s', labels{clustIdx(idx)}) ;
        };
    end

end

Here is how it looks like in both 2D and 3D views (with different display styles):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!