plotyy how to make yaxis align at zero
190 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sandra
am 9 Mai 2012
Kommentiert: Mathias Magdowski
am 28 Mär. 2022
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
0 Kommentare
Akzeptierte Antwort
Geoff
am 9 Mai 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.
Weitere Antworten (4)
Zhonglu Lin
am 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 Kommentare
Sanders A.
am 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
am 21 Sep. 2020
Can you prove it by starting a new question and attaching your script, data, and screenshot?
Davide Fenucci
am 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
0 Kommentare
Image Analyst
am 7 Jan. 2017
Bearbeitet: Image Analyst
am 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;
0 Kommentare
Juan Miguel Serrano Rodríguez
am 29 Okt. 2021
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 Kommentare
Mathias Magdowski
am 28 Mär. 2022
This is a trivial answer. If both axis limits match, one usually does not need a plot with two y axes.
Siehe auch
Kategorien
Mehr zu Two y-axis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!