293 views (last 30 days)
Show older comments
sadel on 14 Jun 2011
Commented: Walter Roberson on 28 Sep 2021
Accepted Answer: sadel
I use the "hold" function to hold the previous plots. But how can I hold the previous legends, too?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
sadel on 17 Jun 2011
Open in MATLAB Online
here is the answer that I was looking for:
x=0:.01:10;
plot(x, sin(x), 'DisplayName','sin');
legend('-DynamicLegend');
hold all; % add new plot lines on top of previous ones
plot(x, cos(x), 'DisplayName','cos');
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
Wycliff Dembe on 7 Feb 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794519
This got the job done. Thanks
Walter Roberson on 7 Feb 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794528
Open in MATLAB Online
In sufficiently new MATLAB, if you called legend without passing in a list of graphics objects, then MATLAB will automatically add new objects created after that point to the legend. Occasionally you may need to
legend show
But back when the question was originally asked, the method shown here was best.
Michael Sonnenberg on 24 Apr 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_834080
Thanks for the question and answer.
Paul Gwambe on 17 Sep 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1013494
Thank you, it worked for me.
Harshia Mathur on 28 Sep 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758229
worked for me as well, Do you know anyway to customise this? I want to append a string, otherwise all it's saying is data1 , data2 etc..
Walter Roberson on 28 Sep 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758359
Open in MATLAB Online
@Harshia Mathur example
x=0:.01:10;
for alpha = [1 5 9]
plot(x, nthroot(sin(x).^alpha,7), 'DisplayName', "sin^{" + (alpha/7) + "}");
hold on;
end
hold off
ylim auto
legend show
Sign in to comment.
More Answers (3)
Gerd on 14 Jun 2011
Open in MATLAB Online
For example I you would like to show the sin and cos you would use
t=[0:0.1:10]
x=sin(t)
y=cos(t)
plot(t,x)
hold
plot(t,y,'r')
legend('sin','cos')
In your case you have to build the legendstring with the two different "strings" you want to show.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Gerd on 14 Jun 2011
Open in MATLAB Online
Hi sadel, to get the current legend use
% you have to have a legend on the current plot
get(legend(gca),'String'); % get legend from current axes.
% Now you can set the legend of the new plot
Gerd
5 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
sadel on 14 Jun 2011
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20656
Hi Gerd and thank you for your reply!
My code is on a button and is something like this:
if get(handles.holdaxes,'value')==1
hold on
get(legend(gca),'String');
else
hold off
end
switch get(val1)
case 1
string='vvsdv'
plot 1
legend(string)
case 2
string='vdsvsdvs'
plot 2
legend(string)
end
when I click the button for second time, my program just change the string of the legend but I want to have 2 lines legend. How can I do this?
Gerd on 14 Jun 2011
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20666
Hi sadel, what do you mean by having a 2 lines legend?
Do you want to have 2 plots with 2 legend?
Gerd
sadel on 14 Jun 2011
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20667
yes
Rajani Metri on 5 Dec 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185118
Hello,
but legend holding only text. It is showing legend indicators as lines, instead of asteric lines, which I used. It is not holding the marking points of plots. For holding Marker points of respective plots, what to do?
Thank You.
Walter Roberson on 5 Dec 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185688
Which MATLAB version are you using? And please explain what you are trying to do in more detail?
Sign in to comment.
Andrea Cavallini on 6 Feb 2018
Quick and dirty... at last (before hold off), draw something out of the scope of the plot, but with the right legend symbols. That will do
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Find more on Legend in Help Center and File Exchange
Tags
- hold legend
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office