plotyy how to make yaxis align at zero
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (4)
Zhonglu Lin
le 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 commentaires
Toby Snaire
le 30 Mai 2020
Awesome - this helped me out too. Copy-paste, switch 'left' and 'right' in my case, and boom!
Image Analyst
le 30 Mai 2020
There is a ylim() function you know. It's simpler and more direct than using the set() function. Of course I think my answer was the simplest and most direct to match the axes limts of the two different axes:
h = plotyy(x, y1, x, y2) % Gets two handles
h(2).YLim = h(1).YLim; % Set the right one equal to the left one.
Sanders A.
le 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
le 21 Sep 2020
Can you prove it by starting a new question and attaching your script, data, and screenshot?
Davide Fenucci
le 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
le 7 Jan 2017
Modifié(e) : Image Analyst
le 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
le 29 Oct 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 commentaires
Jun Liu
le 21 Jan 2022
that didn't work
Mathias Magdowski
le 28 Mar 2022
This is a trivial answer. If both axis limits match, one usually does not need a plot with two y axes.
Catégories
En savoir plus sur Two y-axis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!