Eliminate Duplicates in a Legend
29 次查看(过去 30 天)
显示 更早的评论
Hello!
I am trying to plot 27 points that fit into 12 categories and then have a legend showing what symbols are used for each of the categories. I'm probably overcomplicating this whole thing, but here is the code (abbreviated)
figure
for n = 1:num_stopes % There are 27 of them
ppv = PPV(n); % Vibration Level
this_month = month(n); % Month Number
recep = str2double(regexprep(receptor_list{n},'R','')); % Get Receptor Number. receptor_list is cell array
switch recep % There are 12 receptors
case recep_nums(1)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','r','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(2)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','g','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(3)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','b','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(4)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','m','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(5)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','c','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(6)
plot(this_month,ppv,'Marker','o','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','y','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(7)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','r','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(8)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','g','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(9)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','b','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(10)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','m','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(11)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','c','DisplayName',['Receptor ',num2str(recep)]);
case recep_nums(12)
plot(this_month,ppv,'Marker','diamond','LineStyle','none','MarkerEdgeColor','k','MarkerFaceColor','y','DisplayName',['Receptor ',num2str(recep)]);
end
hold on
end
legend('show');
ylim([0 2.5]);
ylabel('PPV (in/s)')
xlabel('Month')
title('PPV for Receptors Close to Early Stopes')
Here is the resultant figure and legend:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600060/image.jpeg)
Thank you for any help!
Doug Anderson
0 个评论
采纳的回答
DGM
2021-4-28
Because you're looping through the list of datapoints and plotting each one individually, there are not 12 objects, but num_stopes objects. The better way to do this would be to plot all the datapoints associated with a given receptor at once. In other words, instead of building a loop structure like this:
for n=1:num_stopes
% plot this one point
end
do something like
for n=1:num_receptors
% find all points associated with this receptor
% plot this subset of points
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!