Borrar filtros
Borrar filtros

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

263 visualizaciones (últimos 30 días)
sadel
sadel el 14 de Jun. de 2011
Comentada: Walter Roberson el 28 de Sept. de 2021
I use the "hold" function to hold the previous plots. But how can I hold the previous legends, too?

Respuesta aceptada

sadel
sadel el 17 de Jun. de 2011
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 comentarios
Harshia Mathur
Harshia Mathur el 28 de Sept. de 2021
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 el 28 de Sept. de 2021
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

Iniciar sesión para comentar.

Más respuestas (3)

Gerd
Gerd el 14 de Jun. de 2011
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 el 14 de Jun. de 2011
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 comentarios
Rajani Metri
Rajani Metri el 5 de Dic. de 2020
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 el 5 de Dic. de 2020
Which MATLAB version are you using? And please explain what you are trying to do in more detail?

Iniciar sesión para comentar.


Andrea Cavallini
Andrea Cavallini el 6 de Feb. de 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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by