plottyy and linkprop, xtick overlaping with two set of data contain large axis range difference

Hi, I tried to use plotyy to create a 2D plot, I have 2 set of completely different data.and their x axis range is quite large as well, so consider the following code
x1=[1:10];
x2=[5:500000];
y1=2*x1;
y2=(x2).^2;
[AX,H1,H2] = plotyy(x1,y1,x2,y2);
linkprop(AX,{'Xlim','XTickLabel','Xtick'});
datetick(AX(1));
datetick(AX(2));
it only shows one graph at a time, I cant make them appear at the same time and with the same synchronize axis, which does not affect their original axis, any helps, really appreciate that.
anyone please, any hint

Answers (1)

I tried your code out.
Both lines are plotted, but (x1,y1) is clearly flattened on the y axis (leftmost part of the window).
That makes sense, since the x ranges differ by a factor 5e4.
Plotyy basically takes care of the y axis. It makes sense when the x ranges are comparable but the y ranges are not.
In your case, they are both extremely different.
If I'm getting your problem right, you want both graphs "in the same axis"
The only way I see is to do something similar to plotyy, i.e. overlapping two different axes (use the "axes" command with the same position properties for that), and using the bottom and left axis for the first, and the top and bottom axis for the second (I'm referring to the axes properties XAxisLocation and YAxisLocation).
UPDATE This is what I mean
a1=axes;
plot(x1,y1);
a2=axes('XAxisLocation','top','YaxisLocation','right','color','none');
hold on;
plot(x2,y2,'r');
I noticed I have to hold on, otherwise the plot cancels the settings for the axes. It works also like this
a1=axes;
plot(x1,y1);
a2 = axes;
plot(x2,y2,'r');
set(a2,'XAxisLocation','top','YaxisLocation','right','color','none');

8 Comments

by the way, what is the axis on the top of the graph about
your two data sets differ significantly both in the x and y range. Specifically, x1 is in [1, 10], x2 is in [5 5e5].
This means that the xlim for (x1,y1) is 5e-4 smaller than that for (x2,y2). If you use the same xlim for both, you're not going to see x1 (or you'll se only a small portion of x2)
In the code I wrote, the top axis is for x2, the bottom one for x1.
But at this point I'm afraid I did not understand what is exactly that you want to achieve.
yes, I got what you were saying, I know the data differ quite lot, that is why I want to synchronize them, say I want to see both result of y1 and y2 with same x value, so I only want one axis, but it can represent both x1 and x2, like somehow combine them together to give one single axis, is that clear to you now, thanks
I think the codes I have sent you do exactly that.
My point is simply that you need to specify the x axis for the two sets of data, right?
In my example the bottom x axis refers to the blue line, the top x axis refers to the red line.
Is that clear to you now? Try this
a1=axes;
plot(x1,y1,'b');
set(gca,'Xcolor','b','Ycolor','b');
a2 = axes;
plot(x2,y2,'r');
set(a2,'XAxisLocation','top','YaxisLocation','right','color','none','Xcolor','r','Ycolor','r');
box off
PS a different possibility is to represent x2,y2 using rescaled units. If you plot y2 versus x2/5e4 you can use plotyy.
sorry, I guess I haven't made it clear, I have tried your code. but it simply just combine the two function with different axis into one graph, but the x axis is not inline say, when x=5 the x1 and x2 are not in the same position, x2 is shifted or scaled away from x1, that is not what I want, I want them to share the same axis, and only one axis,that is why I tried to use linkprop to link the axis, but you are right, I need to define a limit, like a combined x axis limit that both function can use, thanks for your time anyway,appreciate that
I do not understand how that is possible.
In your example x1 and x2 have a tiny overlap (as compared to the extent of x2). Even worse, y1 and y2 do not overlap at all.
I do not really understand what is your goal here.
Perhaps this is what you want
plot(x1,y1,'b');
hold
plot(x2,y2,'r');
axis([1 10 1 30]);
But you're going to loose most of the second data set.
yes, that what I want, same axis, but I think there is another I can do it, how about time instead of number is it possible, datetick?
Well, I guess so. For instance
datetick('x','dd-mm-yy')
works for me. The only problem is that all those numbers correspond to 1st of January 2000.
I'm not sure in what sense your x data represent dates. But probably the data in your example is not your real data.
What are your real x data? Are they obtained through the datenum function?

Sign in to comment.

Asked:

on 22 Apr 2015

Edited:

pfb
on 22 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!