plottyy and linkprop, xtick overlaping with two set of data contain large axis range difference
Show older comments
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
Victor Lin
on 22 Apr 2015
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.
Victor Lin
on 22 Apr 2015
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.
Victor Lin
on 22 Apr 2015
pfb
on 22 Apr 2015
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.
Victor Lin
on 22 Apr 2015
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?
Categories
Find more on Grid Lines, Tick Values, and Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!