plotyy how to make yaxis align at zero

135 views (last 30 days)
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

Accepted Answer

Geoff
Geoff on 9 May 2012
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 Comment
Sandra
Sandra on 9 May 2012
Thank you so much. That does the job perfectly and is all I needed!!!
YAAYY

Sign in to comment.

More Answers (4)

Zhonglu Lin
Zhonglu Lin on 7 Jan 2017
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 Comments
Sanders A.
Sanders A. on 21 Sep 2020
Image Analyst: but I have vastly differnt scales for my left and right sides so your method doesn't work.
Image Analyst
Image Analyst on 21 Sep 2020
Can you prove it by starting a new question and attaching your script, data, and screenshot?

Sign in to comment.


Davide Fenucci
Davide Fenucci on 24 Sep 2021
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 on 7 Jan 2017
Edited: Image Analyst on 7 Jan 2017
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
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 Comments
Mathias Magdowski
Mathias Magdowski on 28 Mar 2022
This is a trivial answer. If both axis limits match, one usually does not need a plot with two y axes.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!