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:
problem with Plot and Line

Subject: problem with Plot and Line

From: Jonathan

Date: 6 Oct, 2010 20:53:04

Message: 1 of 4

Ok, I'm trying to plot a very simple relationship (x,y). I would like to then show the mean position as a straight line. Here's my code...

plot(B_1,time,'--k','Marker','o','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',9)
hold on
xmin=min(B_1);
xmax=max(B_1);
ymin=731947;
ymax=734504;
axis([xmin,xmax,ymin,ymax]);
line([0 0],[ymin ymax],'LineStyle',':','Color','b','LineWidth',2) %plot line at zero
a=mean(B_1) %find the mean value
line([a a],[ymin ymax],'LineStyle','--','LineWidth',3,'Color',[.8 .8 .8]);
set(gca,'YTick',[731947 732313 732678 733043 733408 733774 734140])
set(gca,'YTickLabel',{'2004' '2005' '2006' '2007' '2008' '2009' '2010'})

The problem lies with second 'line' statement, near the bottom of the script. I don't understand why matlab is refusing to plot the line at location mean? What it is doing is replacing the line at zero. Am I missing something here?

Thanks
Jon

Subject: problem with Plot and Line

From: Sean

Date: 6 Oct, 2010 21:06:20

Message: 2 of 4

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <i8injg$5ip$1@fred.mathworks.com>...
> Ok, I'm trying to plot a very simple relationship (x,y). I would like to then show the mean position as a straight line. Here's my code...
>
> plot(B_1,time,'--k','Marker','o','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',9)
> hold on
> xmin=min(B_1);
> xmax=max(B_1);
> ymin=731947;
> ymax=734504;
> axis([xmin,xmax,ymin,ymax]);
> line([0 0],[ymin ymax],'LineStyle',':','Color','b','LineWidth',2) %plot line at zero
> a=mean(B_1) %find the mean value
> line([a a],[ymin ymax],'LineStyle','--','LineWidth',3,'Color',[.8 .8 .8]);
> set(gca,'YTick',[731947 732313 732678 733043 733408 733774 734140])
> set(gca,'YTickLabel',{'2004' '2005' '2006' '2007' '2008' '2009' '2010'})
>
> The problem lies with second 'line' statement, near the bottom of the script. I don't understand why matlab is refusing to plot the line at location mean? What it is doing is replacing the line at zero. Am I missing something here?
>
> Thanks
> Jon

I think it's drawing your line, it's just outside of your axis.

Work your way up. Only plot the main part. Then add each additional line, without using the axis command or the tick marks...

Subject: problem with Plot and Line

From: Jonathan

Date: 6 Oct, 2010 22:58:05

Message: 3 of 4

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8iocc$qa0$1@fred.mathworks.com>...
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <i8injg$5ip$1@fred.mathworks.com>...
> > Ok, I'm trying to plot a very simple relationship (x,y). I would like to then show the mean position as a straight line. Here's my code...
> >
> > plot(B_1,time,'--k','Marker','o','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',9)
> > hold on
> > xmin=min(B_1);
> > xmax=max(B_1);
> > ymin=731947;
> > ymax=734504;
> > axis([xmin,xmax,ymin,ymax]);
> > line([0 0],[ymin ymax],'LineStyle',':','Color','b','LineWidth',2) %plot line at zero
> > a=mean(B_1) %find the mean value
> > line([a a],[ymin ymax],'LineStyle','--','LineWidth',3,'Color',[.8 .8 .8]);
> > set(gca,'YTick',[731947 732313 732678 733043 733408 733774 734140])
> > set(gca,'YTickLabel',{'2004' '2005' '2006' '2007' '2008' '2009' '2010'})
> >
> > The problem lies with second 'line' statement, near the bottom of the script. I don't understand why matlab is refusing to plot the line at location mean? What it is doing is replacing the line at zero. Am I missing something here?
> >
> > Thanks
> > Jon
>
> I think it's drawing your line, it's just outside of your axis.
>
> Work your way up. Only plot the main part. Then add each additional line, without using the axis command or the tick marks...

If I specify the x values determined by the mean value directly it plots the line correctly. However, if I use 'a' to specify mean it does not seem to like it.
I did notice that the a-value is in scientific notation. Would this have an effect?

Subject: problem with Plot and Line

From: Jan Simon

Date: 7 Oct, 2010 00:12:04

Message: 4 of 4

Dear Jonathan,

> If I specify the x values determined by the mean value directly it plots the line correctly. However, if I use 'a' to specify mean it does not seem to like it.
> I did notice that the a-value is in scientific notation. Would this have an effect?

No, the notation of the floating point numbers is not meaningful for calculations. It is just the appearence in the command window.

Your code looks ok. Please explain exactly what "does not seem to like it" means. Use the debugger to inspect your variable "a" directly before drawing.

Kind regards, Jan

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