Removing a particular legend from plot

2 views (last 30 days)
Joy Mondal
Joy Mondal on 24 Nov 2021
Commented: Star Strider on 24 Nov 2021
clear all
clc
k1=0.7;
k2=01;
wf=1;
ws=1.5;
b=0.15;
d=0.15;
a=0.15;
h=0.05;
j=0;
l=0;
ns1=0;
ns2=0;
nu=0;
for Zf= 0.01:0.01:1.5
n=0;
zf=Zf;
u7=-pi;
u5=pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1);
u3=(-pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)));
u2=8*b*d*wf*ws^2*zf;
u1=pi*(wf^2*ws^2 - k1*k2*ws^2);
v6=pi*(a + h + 2*wf*zf);
v4=(-pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)));
v3=4*b*d*ws^2;
v2=pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2);
v1=(-4*b*d*wf^2*ws^2);
j4=v3*u7;
j3=v3*u5+v1*u7-u2*v6;
j2=v3*u3+v1*u5-u2*v4;
j1=v3*u1+v1*u3-u2*v2;
j0=v1*u1;
j=[j4 j3 j2 j1 j0];
jr=roots(j);
w=jr.^0.5;
for i=1:size(w)
if imag(w(i,1))==0
o=w(i,1);
A=-u2*o^2/(u7*o^7+u5*o^5+u3*o^3+u1*o);
if A>0
d1=6*A*pi*(a + h + 2*wf*zf)*o^5 + (-4*A*pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)))*o^3 + 12*b*d*ws^2*o^2 + 2*A*pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o - 4*b*d*wf^2*ws^2;
d2=- pi*o^7 + pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^5 - pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a))*o^3 + pi*(wf^2*ws^2 - k1*k2*ws^2)*o;
d3=(-7*pi*A)*o^6 + 5*A*pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^4 + (-3*A*pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)))*o^2 + 16*b*d*wf*ws^2*zf*o + pi*A*(wf^2*ws^2 - k1*k2*ws^2);
d4=pi*(a + h + 2*wf*zf)*o^6 - pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1))*o^4 + pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o^2;
D=d1*d2-d3*d4;
l=l+1;
R(l,1)=A;
R(l,2)=o;
R(l,3)=D/abs(D);
end
end
end
R=sortrows(R,-1);
for k=1:size(R,1)
if R(k,3)==1
n=n+1;
if n==1;
ns1=ns1+1;
As1(ns1)=R(k,1);
Zfs1(ns1)=zf;
elseif n==2
ns2=ns2+1;
As2(ns2)=R(k,1);
Zfs2(ns2)=zf;
end
end
if R(k,3)==-1
nu=nu+1;
Au(nu)=R(k,1);
Zfu(nu)=zf;
end
end
R=0;l=0;
end
if ns1>0
plot(Zfs1,As1,'-k')
hold on
end
if ns2>0
plot(Zfs2,As2,'-k')
hold on
end
if nu>0
plot(Zfu,Au,':k')
end
legend
You can see the plot data 1 and data 2 are ploted with same property of line therefore i want to rmove the legend even the legend mark from data 2

Accepted Answer

Star Strider
Star Strider on 24 Nov 2021
The first two lines have the same line style.
To see them individually, choose a different line style for each.
To label them in the legend, use the DisplayName option as described in Specify Legend Labels During Plotting Commands.
I did that in this minor revision of the posted code —
k1=0.7;
k2=01;
wf=1;
ws=1.5;
b=0.15;
d=0.15;
a=0.15;
h=0.05;
j=0;
l=0;
ns1=0;
ns2=0;
nu=0;
for Zf= 0.01:0.01:1.5
n=0;
zf=Zf;
u7=-pi;
u5=pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1);
u3=(-pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)));
u2=8*b*d*wf*ws^2*zf;
u1=pi*(wf^2*ws^2 - k1*k2*ws^2);
v6=pi*(a + h + 2*wf*zf);
v4=(-pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)));
v3=4*b*d*ws^2;
v2=pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2);
v1=(-4*b*d*wf^2*ws^2);
j4=v3*u7;
j3=v3*u5+v1*u7-u2*v6;
j2=v3*u3+v1*u5-u2*v4;
j1=v3*u1+v1*u3-u2*v2;
j0=v1*u1;
j=[j4 j3 j2 j1 j0];
jr=roots(j);
w=jr.^0.5;
for i=1:size(w)
if imag(w(i,1))==0
o=w(i,1);
A=-u2*o^2/(u7*o^7+u5*o^5+u3*o^3+u1*o);
if A>0
d1=6*A*pi*(a + h + 2*wf*zf)*o^5 + (-4*A*pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)))*o^3 + 12*b*d*ws^2*o^2 + 2*A*pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o - 4*b*d*wf^2*ws^2;
d2=- pi*o^7 + pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^5 - pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a))*o^3 + pi*(wf^2*ws^2 - k1*k2*ws^2)*o;
d3=(-7*pi*A)*o^6 + 5*A*pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^4 + (-3*A*pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)))*o^2 + 16*b*d*wf*ws^2*zf*o + pi*A*(wf^2*ws^2 - k1*k2*ws^2);
d4=pi*(a + h + 2*wf*zf)*o^6 - pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1))*o^4 + pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o^2;
D=d1*d2-d3*d4;
l=l+1;
R(l,1)=A;
R(l,2)=o;
R(l,3)=D/abs(D);
end
end
end
R=sortrows(R,-1);
for k=1:size(R,1)
if R(k,3)==1
n=n+1;
if n==1;
ns1=ns1+1;
As1(ns1)=R(k,1);
Zfs1(ns1)=zf;
elseif n==2
ns2=ns2+1;
As2(ns2)=R(k,1);
Zfs2(ns2)=zf;
end
end
if R(k,3)==-1
nu=nu+1;
Au(nu)=R(k,1);
Zfu(nu)=zf;
end
end
R=0;l=0;
end
if ns1>0
plot(Zfs1,As1,'-k', 'DisplayName','ns_1 > 0')
hold on
end
if ns2>0
plot(Zfs2,As2,'-k', 'DisplayName','ns_2 > 0')
hold on
end
if nu>0
plot(Zfu,Au,':k', 'DisplayName','nu > 0')
end
legend
.
  4 Comments
Star Strider
Star Strider on 24 Nov 2021
That was not obvious before.
That solution is straightforward. Change the if block and plot calls respectively to —
if ns1>0
hp{1} = plot(Zfs1,As1,'-k', 'DisplayName','ns_1 > 0');
hold on
end
if ns2>0
hp{2} = plot(Zfs2,As2,'-k', 'DisplayName','ns_2 > 0');
hold on
end
if nu>0
hp{3} = plot(Zfu,Au,':k', 'DisplayName','nu > 0');
end
legend([hp{[1 2]}], 'Location','best')
Note that ‘hp{3}’ exists if using it later is desired, although it is not used here. See Included Subset of Graphics Objects in Legend to documentation on that legend option.
I already made those changes in this version of the code, and those results are displayed in the plot
k1=0.7;
k2=01;
wf=1;
ws=1.5;
b=0.15;
d=0.15;
a=0.15;
h=0.05;
j=0;
l=0;
ns1=0;
ns2=0;
nu=0;
for Zf= 0.01:0.01:1.5
n=0;
zf=Zf;
u7=-pi;
u5=pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1);
u3=(-pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)));
u2=8*b*d*wf*ws^2*zf;
u1=pi*(wf^2*ws^2 - k1*k2*ws^2);
v6=pi*(a + h + 2*wf*zf);
v4=(-pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)));
v3=4*b*d*ws^2;
v2=pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2);
v1=(-4*b*d*wf^2*ws^2);
j4=v3*u7;
j3=v3*u5+v1*u7-u2*v6;
j2=v3*u3+v1*u5-u2*v4;
j1=v3*u1+v1*u3-u2*v2;
j0=v1*u1;
j=[j4 j3 j2 j1 j0];
jr=roots(j);
w=jr.^0.5;
for i=1:size(w)
if imag(w(i,1))==0
o=w(i,1);
A=-u2*o^2/(u7*o^7+u5*o^5+u3*o^3+u1*o);
if A>0
d1=6*A*pi*(a + h + 2*wf*zf)*o^5 + (-4*A*pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1)))*o^3 + 12*b*d*ws^2*o^2 + 2*A*pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o - 4*b*d*wf^2*ws^2;
d2=- pi*o^7 + pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^5 - pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a))*o^3 + pi*(wf^2*ws^2 - k1*k2*ws^2)*o;
d3=(-7*pi*A)*o^6 + 5*A*pi*(wf^2 + 2*zf*(a + h)*wf + ws^2 + a*h + 1)*o^4 + (-3*A*pi*(wf^2*(ws^2 + a*h + 1) - k1*k2 + ws^2 + 2*wf*zf*(h*ws^2 + a)))*o^2 + 16*b*d*wf*ws^2*zf*o + pi*A*(wf^2*ws^2 - k1*k2*ws^2);
d4=pi*(a + h + 2*wf*zf)*o^6 - pi*(a + wf^2*(a + h) + h*ws^2 + 2*wf*zf*(ws^2 + a*h + 1))*o^4 + pi*(wf^2*(h*ws^2 + a) + 2*wf*ws^2*zf - a*k1*k2)*o^2;
D=d1*d2-d3*d4;
l=l+1;
R(l,1)=A;
R(l,2)=o;
R(l,3)=D/abs(D);
end
end
end
R=sortrows(R,-1);
for k=1:size(R,1)
if R(k,3)==1
n=n+1;
if n==1;
ns1=ns1+1;
As1(ns1)=R(k,1);
Zfs1(ns1)=zf;
elseif n==2
ns2=ns2+1;
As2(ns2)=R(k,1);
Zfs2(ns2)=zf;
end
end
if R(k,3)==-1
nu=nu+1;
Au(nu)=R(k,1);
Zfu(nu)=zf;
end
end
R=0;l=0;
end
if ns1>0
hp{1} = plot(Zfs1,As1,'-k', 'DisplayName','ns_1 > 0');
hold on
end
if ns2>0
hp{2} = plot(Zfs2,As2,'-k', 'DisplayName','ns_2 > 0');
hold on
end
if nu>0
hp{3} = plot(Zfu,Au,':k', 'DisplayName','nu > 0');
end
legend([hp{[1 2]}], 'Location','best')
I still believe that using different line styles would make the plot easier to interpret.
Experiment to get different results.
.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!