MATLAB Answers

hide legend for certain curves in a Matlab plot

28 views (last 30 days)
Guillaume Ryelandt
Guillaume Ryelandt on 9 Apr 2020
Commented: ME on 9 Apr 2020
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;

  1 Comment

Guillaume Ryelandt
Guillaume Ryelandt on 9 Apr 2020
Here is what i get, i want to hide the four last lines.

Sign in to comment.

Accepted Answer

ME
ME on 9 Apr 2020
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.

  2 Comments

Guillaume Ryelandt
Guillaume Ryelandt on 9 Apr 2020
Super cool! it works perfectly, thank you very much!
ME
ME on 9 Apr 2020
No problem. Glad I could be some help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!