What should I do to not show the legend for all the plots?

Here is my code:

N=100; n=50; tau=0.001; h=0.01; lambda=tau/h; mu=lambda/2; u=zeros(N,n); u1=zeros(N,n); u2=zeros(N,n); phi=zeros(n,1); for i=1:n for j=1:N u(j,i)=cos(2*pi*i*(j-1)*h); u1(j,i)=cos(2*pi*i*((j-1)*h-tau)); end for j=2:N u2(j,i)=u(j,i)-lambda*(u(j,i)-u(j-1,i)); end u2(1)=0; phi(i,1)=2*pi*i/N; end uf=zeros(n,1); uf1=zeros(n,1); uf2=zeros(n,1); for i=1:n for j=1:N uf(i,1)=uf(i,1)+(u(j,i)*exp(-1i*(j-1)*phi(i,1)))/100; uf1(i,1)=uf1(i,1)+u1(j,i)*exp(-1i*j*phi(i,1))/100; uf2(i,1)=uf2(i,1)+(u2(j,i)*exp(-1i*(j-1)*phi(i,1)))/100; end end final=zeros(n,1); for i=1:n final(i,1)=-(h/(1i*tau))*(log(uf2(i)/uf(i))); end figure; hold on z=1:1:n; b = real(final(z,1)); %plot(phi(z,1),b,'o'); c = imag(final(z,1)); %plot(phi(z,1),c,'-'); %plot(phi(z,1),0,'-'); plot(phi(z,1),b,'ro',phi(z,1),c,'ko',phi(z,1),0,'k-'); legend('Real','Imaginary'); legend ('Location','NorthWest'); xlabel('Reduced Wavenumber') ylabel('Modified Wavenumber') 

I draw a line at y = 0 for reference. I do not want them to be in legend. But I get this figure: enter image description here How to resolve this?

+2
source share
2 answers

Instead of outputting everything with a single plot command, do the following:

 plot(phi(z,1),b,'ro'); hold on plot(phi(z,1),c,'ro'); hold on plot(phi(z,1),0,'k-'); hold off legend('Real','Imaginary','Location','NorthWest'); xlabel('Reduced Wavenumber') ylabel('Modified Wavenumber') 

You should not have a problem.

The actual reason is that in this case the legend is called differently. A more elegant solution is Magla's answer.

+5
source

Legends in matlab should be called as

 plot(phi(z,1),b,'ro',phi(z,1),c,'ko',phi(z,1),0,'k-'); legend( {'Real','Imaginary'} , 'Location', 'NorthWest'); 

where legend labels are stored in an array of row cells {...} . Location is a parameter that should be placed either with a legend call (as in the above code), or outside the function using the set function

 h = legend({'Real','Imaginary'}); set(h, 'Location','NorthWest'); 

This gives

enter image description here

+3
source

All Articles