Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
legend

Subject: legend

From: Snow White

Date: 3 Apr, 2009 10:15:03

Message: 1 of 10

Hello,

I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!

if (f_flag==1)
                         p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
                         hold on;
% legend('plain');
                    else if (f_flag==2)
                             o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
                             hold on;
% legend('oil');
                             else if (f_flag==3)
                                     v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
                                     hold on;
% legend('vaseline');
                                 end

can anyone help me with this?

Bye
                         end
              end

Subject: legend

From: Rafael Herrejon

Date: 3 Apr, 2009 10:42:01

Message: 2 of 10

> Hello,
>
> I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
>
> if (f_flag==1)
> p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> hold on;
> % legend('plain');
> else if (f_flag==2)
> o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> hold on;
> % legend('oil');
> else if (f_flag==3)
> v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> hold on;
> % legend('vaseline');
> end
>
> can anyone help me with this?
>
> Bye
> end
> end

Try this

figure
hold on;
if (f_flag==1)
                         count=count+1;
                         p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
                         set(p(count),'DisplayName','plain');
                    else if (f_flag==2)
                             count=count+1;
                             p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
                             set(p(count),'DisplayName','oil');
                             else if (f_flag==3)
                                     count=count+1;
                                     p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
                                     set(p(count),'DisplayName','vaseline');
                             end
                     end
end
hold off
legend(p,'Location','Best')

Subject: legend

From: Snow White

Date: 3 Apr, 2009 11:14:02

Message: 3 of 10

i get the following error

??? Error using ==> legend>process_inputs at 569
Invalid argument. Type 'help legend' for more information.

Error in ==> legend>make_legend at 330
[orient,location,position,children,listen,strings,propargs] = process_inputs(ha,argin);

Error in ==> legend at 288
    [h,msg] = make_legend(ha,varargin(arg:end));

Error in ==> refractive_index_estimation at 143
                legend(p,'Location','Best');

"Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp> wrote in message <gr4p5p$hu8$1@fred.mathworks.com>...
> > Hello,
> >
> > I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
> >
> > if (f_flag==1)
> > p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> > hold on;
> > % legend('plain');
> > else if (f_flag==2)
> > o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> > hold on;
> > % legend('oil');
> > else if (f_flag==3)
> > v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> > hold on;
> > % legend('vaseline');
> > end
> >
> > can anyone help me with this?
> >
> > Bye
> > end
> > end
>
> Try this
>
> figure
> hold on;
> if (f_flag==1)
> count=count+1;
> p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> set(p(count),'DisplayName','plain');
> else if (f_flag==2)
> count=count+1;
> p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> set(p(count),'DisplayName','oil');
> else if (f_flag==3)
> count=count+1;
> p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> set(p(count),'DisplayName','vaseline');
> end
> end
> end
> hold off
> legend(p,'Location','Best')

Subject: legend

From: Rafael Herrejon

Date: 3 Apr, 2009 11:42:01

Message: 4 of 10

try with legend(p) only. Is any of the f_flag()==1? if not it wont work this way
 

Subject: legend

From: Rafael Herrejon

Date: 3 Apr, 2009 11:50:17

Message: 5 of 10

"Snow White" <gulesaman@gmail.com> wrote in message <gr4nj7$9me$1@fred.mathworks.com>...
> Hello,
>
> I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
>
> if (f_flag==1)
> p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> hold on;
> % legend('plain');
> else if (f_flag==2)
> o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> hold on;
> % legend('oil');
> else if (f_flag==3)
> v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> hold on;
> % legend('vaseline');
> end
>
> can anyone help me with this?
>
> Bye
> end
> end

sorry, my mistake, check that in your code, f_flag can only be one value at one time, wether 1,2 or 3. so how do you want to plot the 3 different values at the same time?

Subject: legend

From: Snow White

Date: 3 Apr, 2009 13:34:01

Message: 6 of 10

i have tried that as well same error and yes f_flag is 1

"Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp> wrote in message <gr4t5p$7t1$1@fred.mathworks.com>...
> "Snow White" <gulesaman@gmail.com> wrote in message <gr4nj7$9me$1@fred.mathworks.com>...
> > Hello,
> >
> > I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
> >
> > if (f_flag==1)
> > p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> > hold on;
> > % legend('plain');
> > else if (f_flag==2)
> > o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> > hold on;
> > % legend('oil');
> > else if (f_flag==3)
> > v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> > hold on;
> > % legend('vaseline');
> > end
> >
> > can anyone help me with this?
> >
> > Bye
> > end
> > end
>
> sorry, my mistake, check that in your code, f_flag can only be one value at one time, wether 1,2 or 3. so how do you want to plot the 3 different values at the same time?

Subject: legend

From: Snow White

Date: 3 Apr, 2009 13:42:01

Message: 7 of 10

heres the error

??? Error using ==> legend>process_inputs at 569
Invalid argument. Type 'help legend' for more information.

Error in ==> legend>make_legend at 330
[orient,location,position,children,listen,strings,propargs] = process_inputs(ha,argin);

Error in ==> legend at 288
    [h,msg] = make_legend(ha,varargin(arg:end));

Error in ==> refractive_index_estimation at 137
                 legend(p);



"Snow White" <gulesaman@gmail.com> wrote in message <gr5389$8oq$1@fred.mathworks.com>...
> i have tried that as well same error and yes f_flag is 1
>
> "Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp> wrote in message <gr4t5p$7t1$1@fred.mathworks.com>...
> > "Snow White" <gulesaman@gmail.com> wrote in message <gr4nj7$9me$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
> > >
> > > if (f_flag==1)
> > > p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> > > hold on;
> > > % legend('plain');
> > > else if (f_flag==2)
> > > o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> > > hold on;
> > > % legend('oil');
> > > else if (f_flag==3)
> > > v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> > > hold on;
> > > % legend('vaseline');
> > > end
> > >
> > > can anyone help me with this?
> > >
> > > Bye
> > > end
> > > end
> >
> > sorry, my mistake, check that in your code, f_flag can only be one value at one time, wether 1,2 or 3. so how do you want to plot the 3 different values at the same time?

Subject: legend

From: Rafael Herrejon

Date: 8 Apr, 2009 09:08:01

Message: 8 of 10

snip

this works, cheers

figure
hold on;
p=0;
count=0;
for f_flag=1:3
if (f_flag==1)
                         count=count+1;
                         %p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
                         p(count)=plot(0,0,'-+m');
                         set(p(count),'DisplayName','plain');
                    else if (f_flag==2)
                             count=count+1;
% p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
                             p(count)=plot(1,1,'-or');
                             set(p(count),'DisplayName','oil');
                             else if (f_flag==3)
                                     count=count+1;
% p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
                                     p(count)=plot(2,2,'-*c');
                                     set(p(count),'DisplayName','vaseline');
                             end
                     end
end
end
hold off
legend(p,'Location','Best')

Subject: legend

From: Snow White

Date: 8 Apr, 2009 17:07:01

Message: 9 of 10

Hello,

i tried this but now i dont get that error but it shows only one name in the legend.. oil or vaseline or plain not all

Bye

"Rafael Herrejon" <rafael.erasethis@ic.is.tohoku.ac.jp> wrote in message <grhphh$21l$1@fred.mathworks.com>...
> snip
>
> this works, cheers
>
> figure
> hold on;
> p=0;
> count=0;
> for f_flag=1:3
> if (f_flag==1)
> count=count+1;
> %p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> p(count)=plot(0,0,'-+m');
> set(p(count),'DisplayName','plain');
> else if (f_flag==2)
> count=count+1;
> % p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> p(count)=plot(1,1,'-or');
> set(p(count),'DisplayName','oil');
> else if (f_flag==3)
> count=count+1;
> % p(count)=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> p(count)=plot(2,2,'-*c');
> set(p(count),'DisplayName','vaseline');
> end
> end
> end
> end
> hold off
> legend(p,'Location','Best')

Subject: legend

From: us

Date: 8 Apr, 2009 19:22:26

Message: 10 of 10

"Snow White"
> I want to add a legend to my plot, i am plotting using the following code but when i do it this way it plots the point on different plots and the legend does not show all three entries together just one entry at a time... when i comment out the legend line it plots fine all three plots on one plot!
> if (f_flag==1)
> p=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-+m');
> hold on;
> % legend('plain');
> else if (f_flag==2)
> o=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-or');
> hold on;
> % legend('oil');
> else if (f_flag==3)
> v=plot(polang(file_name),intensity(x,y,file_name,f_flag),'-*c');
> hold on;
> % legend('vaseline');
> end
> can anyone help me with this...

one of the many solutions

     x=1:10;
     y=.5*rand(size(x))+.25;
     maxflg=3;
     lh=nan(maxflg,1);
for i=1:10
     cflg=i; % a simulation...
switch rem(cflg-1,maxflg)+1
case 1
          lh(1)=line(x,i+y,'color',[1,0,0]);
case 2
          lh(2)=line(x,i+y,'color',[0,1,0]);
case 3
          lh(3)=line(x,i+y,'color',[0,0,1]);
end
end
          legend(lh,{'case 1','case 2','case 3'});

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us