How can I force a MATLAB plot with two y-axes to have 0 at the same height for both axes?
10 views (last 30 days)
Joseph Cunningham on 6 Oct 2022
I'm plotting two metrics vs a single x-axis. However, when the plots are output, the 0 for each of the y-axes is at a different vertical position in the graph. How can I cause them to be in the same position? (ie. at the bottom edge of the graph?) For reference, all values plotted are positive. See the attached:
Walter Roberson on 6 Oct 2022
To have the plots have a common origin, you need to adjust the ylim() for one or both plots such that the first value for the second plot has the same proportion relative to its ylim as the first value of the first plot has relative to its ylim.
y1 = randn(50,1);
y2 = randn(50,1) .* sin(2*pi*rand(50,1));
[l1, u1] = bounds(y1);
[l2, u2] = bounds(y2);
[l1, u1; l2, u2]
%(y2(1)-(l2-p))/(u2-(l2-p)) == (y1(1)-l1)/(u1-l1)
p = (y2(1) - l2 + ((l2 - u2)*(l1 - y1(1)))/(l1 - u1))/((l1 - y1(1))/(l1 - u1) - 1);
If my calculations are right, the origins of the two plots now align.