Aligning ytick marks in a yyplot

6 ビュー (過去 30 日間)
Gustavo Goncalves
Gustavo Goncalves 2015 年 11 月 6 日
コメント済み: Gustavo Goncalves 2015 年 11 月 6 日
I am currently trying to make a plot with two y axes using plotyy. However, I needed to change the y limits on both axes. After I changed the y limits the y ticks on the right and left no longer line up, which makes the plot look poor when I turn the grid on. I am wondering if there is a way to line up the YTick marks. I would like for them to be aligned the way they are when you first make the plot with plotyy. Thanks
Here is my sample code:
t = 1:100;
y = exp(-(1/100)*(t-50).^2);
rr = -0.1 + 0.2*rand(100,1);
y = y+rr';
c = cumtrapz(y);
[ax] = plotyy(t,y,t,c);
set(ax(1),'YLim',[0 inf])
set(ax(2),'YLim',[0 inf])
set(ax(1),'YTick',0:.1:1)
set(ax(2),'YTick',0:2:20)
grid on

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 6 日
No. Setting the YLim upper limit to infinity has no documented meaning, and in practice has subtle interactions with "axis tight" (or not) that make it difficult to say what the "right" solution is.
The calculations that you would need to do in order to align the ticks depend upon having finite ylim for both axes.
  3 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 6 日
In that case,
yt1 = get(ax(1), 'YTick');
yL1 = get(ax(1), 'YLim');
t1ratio = (yt1 - yL1(1))./(yL1(2)-yL1(1));
yL2 = get(ax(2), 'YLim');
yt2 = t1ratio .* (yL2(2)-yL2(1)) + yL2(1);
set(ax(2), 'YTick', yt2)
This relies on the YLim being finite (as otherwise the range is infinite and you end up with 0 for the proportional ratios.)
Gustavo Goncalves
Gustavo Goncalves 2015 年 11 月 6 日
Thanks, this solves my problem. I greatly appreciate it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTwo y-axis についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by