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:
Illiterate to MAT LAB/CompSci- Need Legend to match plot

Subject: Illiterate to MAT LAB/CompSci- Need Legend to match plot

From: Victoria Emberle

Date: 19 Oct, 2010 17:30:14

Message: 1 of 2

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i9ak4g$ap3$1@fred.mathworks.com>...
> "Victoria Emberle" <emberley@uvic.ca> wrote in message <i9aaog$t4g$1@fred.mathworks.com>...
> > Hello,
> >
> > I am a co-op student who is struggling with this one project.
> >
> > I have a program code that within the code it allows you to pick a file and then it displays this file on a plot then I have the code repeat itself where you choose another file and this will display on the same plot. The problem is that the legend is all wrong. I want the legend to match the colors of the lines(there will be 3) and match the correct label. I have been struggling for awhile on this. Please Help me:
> >
> > This is a section of the code that plots the figure and this code repeats once.
> >
> > figure(1);
> > %clf;
> > subplot(2,1,1);
> > plot(measuredProfileRawInterpSmoothPos,measuredProfileRawInterpSmoothDose,
> > analysisProfilePos,analysisProfileDose);
> > axis([0 max(measuredProfileRawInterpPos) 0 max(measuredProfileRawInterpDose)*1.1])
> > %set(gcf,'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1]);
> >
> >
> > title('Comparing Profiles')
> > legend('measuredProfileRawInterpSmooth','analysisProfile')
> >
> >
> >
> > hold on
> > subplot(2,1,2); plot(analysisProfilePos,differenceProfile);
> > axis([0 max(measuredProfileRawInterpPos) 0 max(measuredProfileRawInterpDose)*1.1])
> > title('Difference Between Profiles')
> > legend('difference between measured and monte carlo run')
> >
> > hold all
> >
> > ishold
> >
>
> I made code like yours to check out the problem.
>
> x1=1:10;y1=rand(10,1);x2=x1;y2=rand(10,1);x3=x1;y3=rand(10,1);
> figure(1);
> subplot(2,1,1);
> plot(x1,y1,x2,y2);
> axis([0 10 0 1*1.1])
> title('Comparing Profiles')
> legend('measuredProfileRawInterpSmooth','analysisProfile')
> hold on
> subplot(2,1,2); plot(x3,y3);
> axis([0 10 0 1*1.1])
> title('Difference Between Profiles')
> legend('difference between measured and monte carlo run')
> hold all
> ishold
>
> From this code, Iget two subplots. The upper one has blue and green lines, and a legend with blue and green lines in it.
> The lower one has a blue line, and a legend with a blue line.
> That's exactly how legend should work when you use it this way.
>
> What do you want? In what sense do legend and plot not match?
>
> Ross


Thank you for your interest!! So far so good. The legend works for this part. However,I want another line on the same figure so I make the code repeat itself . When this happens the legend does not account for the third line and the third line plots in the same color as one of the lines on the graph and these lines are very similiar so I cannot distinguish between which line is which . I would ideally like the legend to display three of the lines with all different colors and be correct lol?

I hope this makes sense :)

Subject: Illiterate to MAT LAB/CompSci- Need Legend to match plot

From: Sean

Date: 19 Oct, 2010 19:40:07

Message: 2 of 2

> Thank you for your interest!! So far so good. The legend works for this part. However,I want another line on the same figure so I make the code repeat itself . When this happens the legend does not account for the third line and the third line plots in the same color as one of the lines on the graph and these lines are very similiar so I cannot distinguish between which line is which . I would ideally like the legend to display three of the lines with all different colors and be correct lol?
>
> I hope this makes sense :)

The easiest and most straight-forward way is to make all three lines at the same time.

Example:
plot(x1,y1,'r-',x2,y2,'k--',x3,y3,'b');
legend('y1 = f(x1)','y2 = f(x2)','y3 = f(x3)');

Tags for this Thread

No tags are associated with 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