How can I hold the previous "legend" on a plot?

I use the "hold" function to hold the previous plots. But how can I hold the previous legends, too?

 採用された回答

sadel
sadel 2011 年 6 月 17 日

16 投票

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 件のコメント

Wycliff Dembe
Wycliff Dembe 2020 年 2 月 7 日
This got the job done. Thanks
Walter Roberson
Walter Roberson 2020 年 2 月 7 日
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
Michael Sonnenberg 2020 年 4 月 24 日
Thanks for the question and answer.
Paul Gwambe
Paul Gwambe 2020 年 9 月 17 日
Thank you, it worked for me.
Harshia Mathur
Harshia Mathur 2021 年 9 月 28 日
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
Walter Roberson 2021 年 9 月 28 日
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

サインインしてコメントする。

その他の回答 (3 件)

Gerd
Gerd 2011 年 6 月 14 日

2 投票

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.
Gerd
Gerd 2011 年 6 月 14 日

1 投票

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 件のコメント

sadel
sadel 2011 年 6 月 14 日
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
Gerd 2011 年 6 月 14 日
Hi sadel, what do you mean by having a 2 lines legend?
Do you want to have 2 plots with 2 legend?
Gerd
sadel
sadel 2011 年 6 月 14 日
yes
Rajani Metri
Rajani Metri 2020 年 12 月 5 日
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
Walter Roberson 2020 年 12 月 5 日
Which MATLAB version are you using? And please explain what you are trying to do in more detail?

サインインしてコメントする。

Andrea Cavallini
Andrea Cavallini 2018 年 2 月 6 日

0 投票

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

カテゴリ

タグ

質問済み:

2011 年 6 月 14 日

コメント済み:

2021 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by