Using PLOTYY with multiple arrays of different sizes.
4 views (last 30 days)
Show older comments
(x1,y1), (x2,y2), and (x3,y3) are pairs of arrays which each have a different size.
i.e.
size(x1)==size(y2)
size(x1)~=size(x2)).
The elements of (x3,y3) are of a different scale than the rest of the data and thus why I will be using PLOTYY.
If
size(x)==size(y4)==size(y5)
you can do this:
plotyy(x,[y4,y5],x3,y3)
and the result is 2 plots on one y-axis and one on the other.
It would therefore seem that I could do this:
plotyy([x1,x2],[y1,y2],x3,y3)
but that is not the case. I hope I have made my objective clear, and that someone will have some advice.
0 Comments
Answers (2)
dpb
on 23 Jul 2014
...size(x1)~=size(x2))
It would therefore seem that I could do this:
plotyy([x1,x2],[y1,y2],x3,y3)
I don't see why you would think so...would you expect
z=[x1 x2];
on its own to be successful unless size(x1,1)==size(x2,1)??? How would it be supposed to work otherwise?
The simplest way out of your (basically self-created by overthinking the problem) dilemma is to simply do sotoo..
hAx=plotyy(x1,y1,x3,y3); % plot the two axes different scales, save handles
hold on
plot(hAx(1),x2,y2) % add the second set of data to the first axes
then salt to suit line styles, etc., etc., etc., ...
Alternatively, you can use the feature of plot that ignores NaNs and pad the shorter of x1/x2 and y1/y2 with NaN to make them the same length and then plot the concatenation in a single call. Presuming it's x2 that's the shorter (can always write logic to handle it programmatically; this is just demo purposes code),
n=length(x1)-length(x2); % how many short are we?
x2=[x2;nan(n,1)]; % add that many NaNs
y2=[y2;nan(n,1)];
hAx=plotyy([x1 x2],[y1 y2],x3,y3);
Now you can concatenate as they're same length; plot will ignore the NaN silently.
2 Comments
dpb
on 24 Jul 2014
If you're going to do this fairly frequently it could well be worth a few minutes to write a routine that encapsulates the adding of the NaN to the shorter of arrays passed in order to make them commensurate in length for susbequent concatenation for plotyy (or for plot itself, for that matter).
Alternatively, the process that creates the data arrays perhaps could have the smarts built into it to match the previous longest length so they'd already be the same length.
But, concatenation is the only way to get the array form required for any of the plotting routines; they don't operate on cell arrays which would be a nice enhancement.
Neha Talnikar
on 23 Jul 2014
The last command does work provided that size(x1)==size(x2).
plotyy([x1,x2],[y1,y2],x3,y3)
Similar example is available in the documentation under the Examples topic. Refer to the last example: http://www.mathworks.com/help/matlab/ref/plotyy.html
1 Comment
dpb
on 23 Jul 2014
Excepting that's the precise issue raised in the question -- his initial sizes are not the same so he couldn't concatenate.
See Also
Categories
Find more on Two y-axis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!