Marker doesn't appear in plot3() legend

10 ビュー (過去 30 日間)
emily
emily 2020 年 12 月 7 日
編集済み: Adam Danz 2020 年 12 月 7 日
I am plotting a 3d scatterplot.
I don't understand why the markers don't appear in the legend, any suggestion?
colors = colormap('colorcube');
colors = colors(1:5:end,:);
markers = 'osd*p';
figure('Position',[200 200 800 600])
hold on;
for eIDX = 1:size(eList,1)
data = DataPP{strcmp(DataPP.hello,eList{eIDX}),BestfeatList(1:3)};
plot3(data{:,1}, data{:,2}, data{:,3},'Color',colors(eIDX,:),'Marker',markers(eIDX),'Linestyle','none');
end
xlabel(featNames{BestfeatList(1)},'Interpreter','none')
ylabel(featNames{BestfeatList(2)},'Interpreter','none')
zlabel(featNames{BestfeatList(3)},'Interpreter','none')
grid on;
legend(eList,'Location','EastOutside'); legend boxoff
view([30 7])
title('Three best features')
  1 件のコメント
dpb
dpb 2020 年 12 月 7 日
They do for the 'o' marker which is the first in the list and apparently those handles to which the elements in array elist match up to (which is undefined in the code snippet we have here)

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2020 年 12 月 7 日
編集済み: Adam Danz 2020 年 12 月 7 日
I assume eList contains a cell array of character vectors (or a string array) that define the legend strings.
Try this,
hold on;
h = cell(size(eList));
for eIDX = 1:size(eList,1)
data = DataPP{strcmp(DataPP.hello,eList{eIDX}),BestfeatList(1:3)};
h{eIDX} = plot3(data{:,1}, data{:,2}, data{:,3},'Color',colors(eIDX,:),'Marker',markers(eIDX),...
'Linestyle','none','DisplayName',eList{eIDX}); % eList(eIDX) if using a string array
end
% Get first handle of each handle array
handles = cellfun(@(c)c(1),h);
legend(handles, 'Location','EastOutside')
Why does this problem occur?
When you call plot3(), it apprently produces multiple handles, one for each column of data. Since you're only applying 3 strings in the legend function, the legend is only considering the first three handles which are all from the first call to plot3(). Instead, my solution 1) uses DisplayName to assign legend strings,2) stores all handles produced by plot3, 3) extracts the first handle from each handle array produced by plot3(), and 4) supplies the subsampled handle array to the legend function.
  2 件のコメント
emily
emily 2020 年 12 月 7 日
Yes it works perfectly! thank you very much for your explanation I understand it better now.
Indeed I should have explained, elist was a 5x1 cell.
Thanks again :)
Adam Danz
Adam Danz 2020 年 12 月 7 日
編集済み: Adam Danz 2020 年 12 月 7 日
Glad I could help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by