# what is the best way of showing legend?

1 view (last 30 days)
Ham Man on 17 Aug 2022
Commented: Ham Man on 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!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

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

Sign in to comment.

### Categories

Find more on Legend in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!