double axis used in matlab

2019-09-06 18:34发布

问题:

I am using a plotyy in matlab to plot two data set in a same figure. The left and right axis are of the different range. But I just find that on the right axis, there seems to be two different set of scale shown. I think one of them is really from the left axis.

t=-1:0.02:1; 
y=sin(t); 
y1=2*sech(t); 
[AX, H] =plotyy(t, y, t, y1); 
ylim(AX(2), [0 3.25]); 
set(AX(2), 'YTickMode', 'auto')

After searching that online, I found that to turn off the box will solve the problem too. But the problem is to turn the box off will case the top horizontal line gone also. Is that anyway to remove the extra scale and keep the frame? Thanks.

回答1:

I don't think there's simple a way to do what you want. If you turn off the box (to get rid of the blue ticks on the right hand side) then the top horizontal line will disappear:

set(AX(1), 'Box','off')

If you want you could re-draw the line back in with:

line([-1, 1], [1, 1])

Or more generally:

lims = get(AX(1),{'XLim','YLim'});
line(lims{1}, lims{2}([2 2]))


回答2:

It is possible and not terribly difficult, here's an illustrative example figure based on your test code

What I've done is to add a third axis (in this case I accomplished this by taking a shortcut - I called plotyy twice resulting in an extra blue line in the first axis and an extra second axis with a green line).

The idea is to turn the bounding box off for both the first and second axes, and then to turn it on for the third. That results in the top axis giving you the left vertical axis, the second the right vertical axis, and the third the top horizontal axis.



标签: matlab axes