Legend on scatter3 in Matlab

matlab matlab-figure

1384 观看

1回复

40 作者的声誉

I was expecting that to work, but I am missing what is a "vector of handles", from MATLAB helpfile.

LEGEND(M), where M is a string matrix or cell array of strings, and

LEGEND(H,M) where H is a vector of handles to lines and patches also works.

myone = ones(20,1);
mytwo = ones(20,1)+1;
rows = vertcat(myone,mytwo);
mylabels = {'Alpha', 'Beta'};

figure
grouplabels = mylabels(rows);
h = scatter3(rand(40,1),rand(40,1),rand(40,1),20,rows,'filled'), ...
    view(-33,22)
legend(handle(h),grouplabels)

xlabel('X')
ylabel('Y')
zlabel('Z')

enter image description here

作者: owblique 的来源 发布者: 2016 年 5 月 17 日

回应 (1)


1

3046 作者的声誉

决定

The problem with your code is that h, the output of scatter3, is a single handle. It's not an array of handles with the same size as your data (which is what you imply when trying to set 40x1 array of labels on it, ignoring irrelevant handle wrapper). And it's not even an array of two handles as one may have thought (one per color). So you cannot set legend like this. One way around would be to plot all the points of one color at a time:

hFig = figure();
axh = axes('Parent', hFig);
hold(axh, 'all');
h1 = scatter3(rand(20,1),rand(20,1),rand(20,1),20,'b','filled');
h2 = scatter3(rand(20,1),rand(20,1),rand(20,1),20,'r','filled');
view(axh, -33, 22);
grid(axh, 'on');
legend(axh, [h1,h2], {'Alpha', 'Beta'});

enter image description here

作者: nirvana-msu 发布者: 17.05.2016 10:58
32x32