필터 지우기
필터 지우기

plotyy how to make yaxis align at zero

조회 수: 106 (최근 30일)
Sandra
Sandra 2012년 5월 9일
댓글: Mathias Magdowski 2022년 3월 28일
Hello everyone, I am using plotyy and it works fine so far. I was wondering if there is a way to align the two y axes at zero. So that I could plot a third line on top at zero for visual reference. The scale of both variables I am plotting is obviously different but the both range from negative values to positive one. Any ideas? Thank you so much in advance Sandra

채택된 답변

Geoff
Geoff 2012년 5월 9일
Well, if I plot some test data:
[ax, h1, h2] = plotyy([1 2 3], [-1 5 6], [2 3 4], [-2 1 -1]/2);
I get back two axis handles in ax, and also a handle to my two data series. I can set YLim on each of the axes individually. The question is how to adjust YLim such that the zeros both align...
The simplest approach is to put zero in the centre.
maxval = cellfun(@(x) max(abs(x)), get([h1 h2], 'YData'));
ylim = [-maxval, maxval] * 1.1; % Mult by 1.1 to pad out a bit
set(ax(1), 'YLim', ylim(1,:) );
set(ax(2), 'YLim', ylim(2,:) );
But that's not usually a great idea. Your data is probably not symmetric about zero, and/or you might want to put zero in a specific place.
I haven't done this sort of thing for a while, and don't feel like scratching my head over it right now... But the upshot is now you know HOW, you just need to find a suitable formula to align the ranges such that the ratio of zero to the min and max value is the same on both axes.
  댓글 수: 1
Sandra
Sandra 2012년 5월 9일
Thank you so much. That does the job perfectly and is all I needed!!!
YAAYY

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

추가 답변 (4개)

Zhonglu Lin
Zhonglu Lin 2017년 1월 7일
a slightly better one based on Geoff's answer.
%%align zero for left and right
yyaxis right; ylimr = get(gca,'Ylim');ratio = ylimr(1)/ylimr(2);
yyaxis left; yliml = get(gca,'Ylim');
if yliml(2)*ratio<yliml(1)
set(gca,'Ylim',[yliml(2)*ratio yliml(2)])
else
set(gca,'Ylim',[yliml(1) yliml(1)/ratio])
end
  댓글 수: 4
Sanders A.
Sanders A. 2020년 9월 21일
Image Analyst: but I have vastly differnt scales for my left and right sides so your method doesn't work.
Image Analyst
Image Analyst 2020년 9월 21일
Can you prove it by starting a new question and attaching your script, data, and screenshot?

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


Davide Fenucci
Davide Fenucci 2021년 9월 24일
This should work for all the possible combinations
function align_yyaxis_zero(ax)
% align zero for left and right
yyaxis left; yliml = get(ax,'Ylim');
yyaxis right; ylimr = get(ax,'Ylim');
% Remove potential zeros from the limits
yliml = yliml - 0.05 * (yliml == 0) .* yliml([2 1]);
ylimr = ylimr - 0.05 * (ylimr == 0) .* ylimr([2 1]);
if yliml(1) > 0 && ylimr(1) > 0
yliml = [0 yliml(2)];
ylimr = [0 ylimr(2)];
elseif yliml(2) < 0 && ylimr(2) < 0
yliml = [yliml(1), 0];
ylimr = [ylimr(1), 0];
elseif yliml(1) > 0 && ylimr(2) < 0
ratio = diff(yliml)/diff(ylimr);
yliml = [ylimr(1)*ratio, yliml(2)];
ylimr = [ylimr(1), yliml(2)/ratio];
elseif yliml(2) < 0 && ylimr(1) > 0
ratio = diff(yliml)/diff(ylimr);
yliml = [yliml(1), ylimr(2)*ratio];
ylimr = [yliml(1)/ratio, ylimr(2)];
elseif yliml(1) > 0
yliml(1) = yliml(2) * ylimr(1) / ylimr(2);
elseif yliml(2) < 0
yliml(2) = yliml(1) * ylimr(2) / ylimr(1);
elseif ylimr(1) > 0
ylimr(1) = ylimr(2) * yliml(1) / yliml(2);
elseif ylimr(2) < 0
ylimr(2) = ylimr(1) * yliml(2) / yliml(1);
else
dl = diff(yliml);
dr = diff(ylimr);
if yliml(2)/dl > ylimr(2)/dr
ylimr(2) = yliml(2)*dr/dl;
yliml(1) = ylimr(1)*dl/dr;
else
yliml(2) = ylimr(2)*dl/dr;
ylimr(1) = yliml(1)*dr/dl;
end
end
yyaxis left; set(ax, 'YLim', yliml);
yyaxis right; set(ax, 'Ylim', ylimr);
end

Image Analyst
Image Analyst 2017년 1월 7일
편집: Image Analyst 2017년 1월 7일
Try this:
% Sample data
x = 1 : 50
y1 = 2 * x - 30;
y2 = 100 * cos(x/50) - 70;
% Plot, getting back handles to two axes.
h = plotyy(x, y1, x, y2)
grid on;
% Make the x axis of axes #1 go through the Y=0 point.
h(1).XAxisLocation = 'origin';
% Make the y axis range of axes #2 match that of axes #1
% Meaning they will both share the same x axis and the
% x axis will go through the Y=0 point of both the left and right axis.
% OOP programming valid for R2014b and later.
h(2).YLim = h(1).YLim;

Juan Miguel Serrano Rodríguez
Juan Miguel Serrano Rodríguez 2021년 10월 29일
Another alternative using yyaxis:
% First plot
plot(ensayo.TimeStamp, ensayo.(ptop.medidas.Mprod.sensor_id));
ax = gca; hold on;
ylabel("m3/h"); yyaxis right; ylabel("kg/s"); % Labelling
% Second plot
Mprod_kgs = ensayo.(ptop.medidas.Mprod.sensor_id) .* densSatLiqTW( ensayo.(ptop.medidas.Tprod.sensor_id) )/3600;
plot(ensayo.TimeStamp, Mprod_kgs); title("Evolución Mprod");
% Make the axis align
ax.YAxis(2).Limits = ax.YAxis(1).Limits;
  댓글 수: 2
Jun Liu
Jun Liu 2022년 1월 21일
that didn't work
Mathias Magdowski
Mathias Magdowski 2022년 3월 28일
This is a trivial answer. If both axis limits match, one usually does not need a plot with two y axes.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by