plotyy how to make yaxis align at zero
108 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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.
更多回答(4 个)
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.
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
2020-9-21
Can you prove it by starting a new question and attaching your script, data, and screenshot?
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
0 个评论
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;
0 个评论
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 个评论
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 Center 和 File Exchange 中查找有关 Two y-axis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!