what is the best way of showing legend?

2 vues (au cours des 30 derniers jours)
Ham Man
Ham Man le 17 Août 2022
Commenté : Ham Man le 18 Août 2022
could anyone tell me what is the best way of showing legend in this example?
plot(x1,y1,'o-r',-x1,y1,'o-r');hold on;
plot(x2,y2);
%I tried this way:
legend('show');legend('plotx1y1','','plotx2y2');
% but it does not look good!
I appreciate any help!

Réponse acceptée

dpb
dpb le 17 Août 2022
You don't explain what you don't like nor show us what you got and we don't have your data, so we're flying blind here, but I'll guess
plot([-x1(:);x1(:)],[y1(:);y1(:)],'o-r');
hold on;
plot(x2,y2);
legend('plotx1y1','plotx2y2');
or
hL=gobjects(3,1); % preallocate for line handles to come
hL=plot(x1,y1,'o-r',-x1,y1,'o-r'); % first two
hold on;
hL(3)=plot(x2,y2); % and third...
legend(hL([1 3]),'plotx1y1','plotx2y2'); % label the desired ones
or
plot(x1,y1,'o-r','DisplayName','plotx1y1','Annotation','on'); % first, use legend
hold on;
plot(-x1,y1,'o-r','Annotation','off'); % no legend on second
plot(x2,y2,'DisplayName','plotx2y2','Annotation','on'); % show third
legend % turn the legend on
  1 commentaire
Ham Man
Ham Man le 18 Août 2022
Many thanks, I used the first suggested method and it works fine!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by