plotyy : align y axes at zero
4 ビュー (過去 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!