Filter löschen
Filter löschen

what is the best way of showing legend?

3 Ansichten (letzte 30 Tage)
Ham Man
Ham Man am 17 Aug. 2022
Kommentiert: Ham Man am 18 Aug. 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!

Akzeptierte Antwort

dpb
dpb am 17 Aug. 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 Kommentar
Ham Man
Ham Man am 18 Aug. 2022
Many thanks, I used the first suggested method and it works fine!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by