필터 지우기
필터 지우기

How can I force a MATLAB plot with two y-axes to have 0 at the same height for both axes?

조회 수: 73 (최근 30일)
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:
Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 6일
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.
rng(12345)
y1 = randn(50,1);
y2 = randn(50,1) .* sin(2*pi*rand(50,1));
yyaxis right
plot(y1)
ylim auto
yyaxis left
plot(y2)
ylim auto
[l1, u1] = bounds(y1);
[l2, u2] = bounds(y2);
[l1, u1; l2, u2]
ans = 2×2
-2.3876 2.1233 -2.4340 1.1394
%syms p
%(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);
yyaxis right
ylim([l1, u1])
yyaxis left
ylim([l2-p, u2])
If my calculations are right, the origins of the two plots now align.
  댓글 수: 3
Torsten
Torsten 2022년 10월 7일
x = 0:0.1:1;
y1 = x.^2 + 3 ;
y2 = 0.001*x.^2;
yyaxis right
plot(y1)
ylim ([0 4])
yyaxis left
plot(y2)
ylim auto
Joseph Cunningham
Joseph Cunningham 2022년 10월 7일
Thank you Torsten, this was the simple solution that I had somehow overlooked! Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by