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:
plotyy problem with shifted x axis

Subject: plotyy problem with shifted x axis

From: Martin Skodacek

Date: 22 Mar, 2011 18:29:05

Message: 1 of 8

Hello I have troubles with plotyy

I have data in matrix M1 in format f.e.:
2011 01 01 13 56 00 20 20 125 136 0.18 65
first six rows are date time the next are traffic data...
I am computing final
 TimeAxe = datenum(M1(:,1),M1(:,2),M1(:,3),M1(:,4),M1(:,5),M1(:,6));
  plotyy(TimeAxe, M1(:,7), TimeAxe, M1(:,8));
grid;datetick('x',15);

but 1st and second lines are shifted to itself. this is visible on this image
http://tinypic.com/r/2ahe9sh/7

when I delete datetick, graph is normal, can please tell me why is this done and help me to remove shift? I tried

  [AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
         set(get(AX(1),'Ylabel'),'String','Intenzita VJ/km');
         set(get(AX(2),'Ylabel'),'String','Rychlost km/h');
         xlim(AX(2),[min(TimeAxe),max(TimeAxe)])
         linkaxes(AX,'x')

        set(Int,'LineStyle','.','Color','c');datetick('x',15);
        set(Spd,'LineStyle','.','Color','m');datetick('x',15);

but without any success.

Thank you for your help. Martin
P.S. is it possible to set min and max on y axes? sth like using axes(xmin,xmax,ymin,ymax)???
thank you once again for quick answer. Martin

Subject: plotyy problem with shifted x axis

From: Sadik

Date: 23 Mar, 2011 01:54:04

Message: 2 of 8

Hi Martin,

You have two axes to play with so you need their handles. After you open up a new figure and do your plot, do this:

allHandles = get(get(gca,'Parent'),'Children');
% allHandles(1) belongs to your M1(:,8) data and allHandles(2) to M1(:,7)
% So the most recent plot is associated with the first handle.

Now you can do datetick and y axis limit stuff:

datetick(allHandles(1),'x',15);
datetick(allHandles(2),'x',15);

set(allHandles(1),'YLim',[500 2000]) % for example
set(allHandles(2),'YLim',[0 1000])

Best.

Subject: plotyy problem with shifted x axis

From: Sadik

Date: 23 Mar, 2011 02:03:05

Message: 3 of 8

Or, you can use your AX variable above, but the order is not flipped.

Best.

Subject: plotyy problem with shifted x axis

From: Martin Skodacek

Date: 23 Mar, 2011 07:43:05

Message: 4 of 8

Thank you for your help Sadik. I didn´t realised any

This works fine for one graph and combinig with other plots.
But when I want to combine two plotyy using hold on they are still shifted.
Can you help me please define the heritage frm fist graph into second one?

TimeAxe = datenum(M1(:,1),M1(:,2),M1(:,3),M1(:,4),M1(:,5),M1(:,6));
    
    if(get(handles.IntSp_toPlot,'Value')) == 1
        [AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
        allHandles = get(get(gca,'Parent'),'Children');
        set(get(AX(1),'Ylabel'),'String','Intenzita VJ/km'); %set(allHandles(1),'Ylim',[0 7000]);
        set(get(AX(2),'Ylabel'),'String','Rychlost km/h');%set(allHandles(2),'Ylim',[0 200]);
        set(Int,'LineStyle','.','Color','c');datetick(allHandles(1),'x',15);
        set(Spd,'LineStyle','.','Color','m');datetick(allHandles(2),'x',15);
        legend('Intenzita','Rychlost');grid;
        hold on; end

    if(get(handles.smIntSp_toPlot,'Value')) == 1
        [AX,smInt,smSpd] = plotyy(TimeAxe, M1(:,9),TimeAxe, M1(:,10),'plot');
        allHandles1 = get(get(gca,'Parent'),'Children');
        set(get(AX(1),'Ylabel'),'String','Intenzita VJ/km');%set(allHandles1(1),'Ylim',[0 7000]);
        set(get(AX(2),'Ylabel'),'String','Rychlost km/h');%set(allHandles1(2),'Ylim',[0 200]);
        set(smInt,'LineStyle','-','Color','b');datetick(allHandles1(1),'x',15);
        set(smSpd,'LineStyle','-','Color','r');datetick(allHandles1(2),'x',15);
        legend('Intenzita','Rychlost');grid;
        hold on; end

Explanation:
Why I need to do this is because of plotting intensities and occupancies in one graph. And I need to interlay non-smoothed and smoothed graphs. I designed a gui and if checkbox is checked then is graph plotted. they have to be independent (thats why here are if´s). thank you.

Can you both please look at ranges (comennted). they seems to be not working correctly...

Thank you for your help. Martin

Subject: plotyy problem with shifted x axis

From: Sadik

Date: 23 Mar, 2011 11:33:05

Message: 5 of 8

Hi Martin,

If you use allHandles while doing hold on, you may easily lose track. Therefore, it is better to use your AX variables as follows:

[AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
datetick(AX(1),'x',15) % for M(:,7)
datetick(AX(2),'x',15)

[AX2,Int,Spd] = plotyy(TimeAxe, M1(:,9),TimeAxe, M1(:,10),'plot');
datetick(AX2(1),'x',15) % for M(:,9)
datetick(AX2(2),'x',15)

Best.

Subject: plotyy problem with shifted x axis

From: Martin Skodacek

Date: 24 Mar, 2011 12:13:05

Message: 6 of 8

Thank you sadik for your help. plotyy in our appliction works nicely now.
Kind regards, Martin

"Sadik " <sadik.hava@gmail.com> wrote in message <imclph$f4o$1@fred.mathworks.com>...
> Hi Martin,
>
> If you use allHandles while doing hold on, you may easily lose track. Therefore, it is better to use your AX variables as follows:
>
> [AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
> datetick(AX(1),'x',15) % for M(:,7)
> datetick(AX(2),'x',15)
>
> [AX2,Int,Spd] = plotyy(TimeAxe, M1(:,9),TimeAxe, M1(:,10),'plot');
> datetick(AX2(1),'x',15) % for M(:,9)
> datetick(AX2(2),'x',15)
>
> Best.

Subject: plotyy problem with shifted x axis

From: Jessica Lam

Date: 10 Apr, 2011 14:05:05

Message: 7 of 8

I got the same problem. Tried to use your code to fix it. But it does not work.

"Martin Skodacek" wrote in message <imfcgh$53q$1@fred.mathworks.com>...
> Thank you sadik for your help. plotyy in our appliction works nicely now.
> Kind regards, Martin
>
> "Sadik " <sadik.hava@gmail.com> wrote in message <imclph$f4o$1@fred.mathworks.com>...
> > Hi Martin,
> >
> > If you use allHandles while doing hold on, you may easily lose track. Therefore, it is better to use your AX variables as follows:
> >
> > [AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
> > datetick(AX(1),'x',15) % for M(:,7)
> > datetick(AX(2),'x',15)
> >
> > [AX2,Int,Spd] = plotyy(TimeAxe, M1(:,9),TimeAxe, M1(:,10),'plot');
> > datetick(AX2(1),'x',15) % for M(:,9)
> > datetick(AX2(2),'x',15)
> >
> > Best.

Subject: plotyy problem with shifted x axis

From: Martin Skodacek

Date: 10 Apr, 2011 14:54:04

Message: 8 of 8

In my case this works absoluterly, here is part of used code

    [AX,Int,Spd] = plotyy(TimeAxe, M1(:,8),TimeAxe, M1(:,7),'plot');
        set(AX(1),'Ylim',[0 200]); set(AX(2),'Ylim',[0 5000]);
        % set(get(AX(1),'Ylabel'),'String','Rychlost km/h');
        % set(get(AX(2),'Ylabel'),'String','Intenzita v.j/3min');
        set(Int,'LineStyle','.','Color',[0.7 0.7 0.7]);datetick(AX(1),'x',15);
        set(Spd,'LineStyle','.','Color','c');datetick(AX(2),'x',15);
        grid; hold on;

Use this as inspiration, If will not work, give us some part of code. Without that I can´t help you.

Regards, Martin


"Jessica Lam" wrote in message <insdeh$fj6$1@fred.mathworks.com>...
> I got the same problem. Tried to use your code to fix it. But it does not work.
>
> "Martin Skodacek" wrote in message <imfcgh$53q$1@fred.mathworks.com>...
> > Thank you sadik for your help. plotyy in our appliction works nicely now.
> > Kind regards, Martin
> >
> > "Sadik " <sadik.hava@gmail.com> wrote in message <imclph$f4o$1@fred.mathworks.com>...
> > > Hi Martin,
> > >
> > > If you use allHandles while doing hold on, you may easily lose track. Therefore, it is better to use your AX variables as follows:
> > >
> > > [AX,Int,Spd] = plotyy(TimeAxe, M1(:,7),TimeAxe, M1(:,8),'plot');
> > > datetick(AX(1),'x',15) % for M(:,7)
> > > datetick(AX(2),'x',15)
> > >
> > > [AX2,Int,Spd] = plotyy(TimeAxe, M1(:,9),TimeAxe, M1(:,10),'plot');
> > > datetick(AX2(1),'x',15) % for M(:,9)
> > > datetick(AX2(2),'x',15)
> > >
> > > Best.

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