MATLAB: Using plot with 2 x and 2 y axis

plot on multiple axis

Hello, Here is what I would like to do. I have a matrix which is n x 4. I want to utilize plot to create a figure where column 1 & 2 are plotted with the axes on the left and bottom and column 3 & 4 plotted with the axes on the right and top. The x axis values are not identical but would like them both to start at the left axis. I don't have any code examples and I've been able to utilize line to accomplish this, but would like to use plot instead. Thanks..Dave

Best Answer

Just use plotyy and fix the initial result up a little...example
x=[[1:10].' [1:10].'-5];
y=[randn(10,2) randn(10,2)];
hAx=plotyy(x(:,1),y(:,1:2),x(:,2),y(:,3:4));
set(hAx(2),'XAxisLoc','top')
xlim(hAx(1),[min(x(:,1) max(x(:,1)])
xlim(hAx(2),[min(x(:,2) max(x(:,2)])
results in
If you're going to want to do this more than just once or twice, make a new function containing the above and then won't have to do much at all except call it. Be sure, of course, in that case to save and return the other handles from plotyy so you'll have easy access to them to make any other modifications wanted besides the bare bones to begin...