Eliminate Duplicates in a Legend
조회 수: 52 (최근 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:
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!