hide legend for certain curves in a Matlab plot

Hello everyone,
I have a little problem here, i plot 8 different curves on a figure but i want to hide the legend for the four last of them since they are only the new step of my simulation, does anybody has an idea to keep the legend for the first of them but hide it for the 4 last ones
TimeStead=max(TimeMeasurStead,TimeRealStead);
TimeStead=max(TimeStead,TimeVelStead);
xx1 = linspace(0,TimeStead(1),1e3).';
xx2 = linspace(0,TimeStead(2),1e3).';
xx3 = linspace(0,TimeStead(3),1e3).';
xx4 = linspace(0,TimeStead(4),1e3).';
tt1 = linspace(TimeStead(1),2000,1e3).';
tt2 = linspace(TimeStead(2),2000,1e3).';
tt3 = linspace(TimeStead(3),2000,1e3).';
tt4 = linspace(TimeStead(4),2000,1e3).';
tstead1=find(Tout1>TimeStead(1));
tstead2=find(Tout2>TimeStead(2));
tstead3=find(Tout3>TimeStead(3));
tstead4=find(Tout4>TimeStead(4));
M=[0,0,0,0];
IC2=[Xout1(tstead1(1),1);Xout1(tstead1(1),2);Xout1(tstead1(1),3);Xout1(tstead1(1),4)];
IC3=[Xout2(tstead2(1),1);Xout2(tstead2(1),2);Xout2(tstead2(1),3);Xout2(tstead2(1),4)];
IC4=[Xout3(tstead3(1),1);Xout3(tstead3(1),2);Xout3(tstead2(1),3);Xout3(tstead2(1),4)];
IC5=[Xout4(tstead4(1),1);Xout4(tstead4(1),2);Xout4(tstead4(1),3);Xout4(tstead4(1),4)];
opts= odeset('RelTol',1e-8,'AbsTol',1e-10);
[~,Xout11] = ode45(@(t,x)mydyn(t,x,M(1)),tt1,IC2,opts);
[~,Xout21] = ode45(@(t,x)mydyn(t,x,M(2)),tt2,IC3,opts);
[~,Xout31] = ode45(@(t,x)mydyn(t,x,M(3)),tt3,IC4,opts);
[~,Xout41] = ode45(@(t,x)mydyn(t,x,M(4)),tt4,IC5,opts);
%data visualisation
figure(8);
plot(tt1,Xout11(:,1),'b','linewidth',2,'displayName','M= 5 Nm');
xlabel('$t$(s)','interpreter','Latex')
ylabel('$T$(K)','interpreter','Latex')
set(gca,'FontSize',11)
hold on;
plot(tt2,Xout21(:,1),'g','linewidth',2,'displayName','M= 15 Nm');
plot(tt3,Xout31(:,1),'r','linewidth',2,'displayName','M= 25 Nm');
plot(tt4,Xout41(:,1),'k','linewidth',2,'displayName','M= 55 Nm');
plot(xx1,Xout1(:,1),'b','linewidth',2,'displayName','');
plot(xx2,Xout2(:,1),'g','linewidth',2,'displayName','');
plot(xx3,Xout3(:,1),'r','linewidth',2,'displayName','');
plot(xx4,Xout4(:,1),'k','linewidth',2,'displayName','');
lgd=legend;
[TimeSteadFinal,TempSteadFinal]=ginput(4);
TimeSteadFinal=TimeSteadFinal-TimeStead;
hold off;

 Akzeptierte Antwort

ME
ME am 9 Apr. 2020

13 Stimmen

If you aren't fussed about actually doing this within the code then you can select the lines, open up the property editor, find the property called "Handle Visibility" and set this to "Off".
If you want to do this in your code then you can insert 'HandleVisibility','off' instead of 'displayName','' in the plotting commands of those lines you don't want labelled.
I think that should work! If not then comment back on here and let me know.

6 Kommentare

Super cool! it works perfectly, thank you very much!
ME
ME am 9 Apr. 2020
No problem. Glad I could be some help!
Linjun He
Linjun He am 13 Dez. 2021
Neat solution! Thank you!
Matty
Matty am 14 Okt. 2022
Not wroking for me
ME
ME am 14 Okt. 2022
Could you give some more information about what exactly you are doing and where it fails (any error messages or strange behaviours, etc.)? That'll help me to see where this isn't working for you.
JClarcq
JClarcq am 13 Mär. 2024
@ME Thanks it works:
plot(app.UIAxes,X,Y1,'HandleVisibility','off')
plot(app.UIAxes,X,Y2,'DisplayName','Exp')
legend(app.UIAxes)
This hide the curve in the legend for Y1, but add legend 'Exp' for Y2.
Perfect

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

am 9 Apr. 2020

Kommentiert:

am 13 Mär. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by