plotyy : align y axes at zero
3 次查看(过去 30 天)
显示 更早的评论
When using plotyy I need to plot two different series. One on the left y axis, and the other on the right y axis. Both series contain numbers in different scales. The one of the left contains numbers from 0 to (potentially) Infinity, but the one on the right is restricted to the range -1, 1. In the code below, you can see an example.
I'd like to make sure that both axes cross at y = 0, but I'm not sure how to do it. In the example I included, you can see that the two y axes are misaligned.
Thanks in advance
x = 15;
fn1 = @(x, y) bar(x, y, 0.3, 'FaceColor', 'blue');
fn2 = @(x, y) bar(x, y, 0.3, 'FaceColor', 'red');
figure;
plotyy( [1:x]-1, rand(x, 1) * 100, [1:x]+1, 2 * rand(x, 1) - 1, fn1, fn2)
0 个评论
采纳的回答
Oleg Komarov
2012-2-25
A quick fix, call plotyy as:
ax = plotyy(...)
Then set limits of axes
set(ax(1),'Ylim',max(get(ax(1),'Ylim')) * [-1 1])
EDIT General solution with ratio of negative leg over positive
% Retrieve Ylim of second axis
ylim2 = get(ax(2),'Ylim');
% Ratio of negative leg to positive one (keep sign)
ratio = ylim2(1)/ylim2(2);
% Set same ratio for first axis
ylim1 = get(ax(1),'Ylim');
set(ax(1),'Ylim',[ylim1(2)*ratio ylim1(2)])
更多回答(0 个)
另请参阅
类别
在 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!