Link axes with different Y-Scales

조회 수: 22 (최근 30일)
Alexander von Mach
Alexander von Mach 2019년 11월 6일
편집: Adam Danz 2019년 11월 14일
I'm trying to make a plot with 2 y-axes and wasn't able to use the yyaxis command since I want to change the uistack afterwards and it doesn't work with this function showhow. Now I have a new problem because of that. When I try to link the 2 axes, the y-scale of the left axis is changed to the values of the right one that are 10 times bigger.
Is there a way to link the axes to be able to "zoom" or change the parts visible without changing the axis values?
ax2 = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
ax1 = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
plot(ax1,[1 5 10],[1 5 10]); % Just random values not mine
plot(ax2,[1 5 10],[10 50 100]); % Just random values not mine
ax2.YAxisLocation = 'right';
set(ax1, 'Color', 'None');
ax2.XTick = [];
linkaxes([ax1 ax2]);
1.png
2.png
Thanks for your help
  댓글 수: 2
Adam Danz
Adam Danz 2019년 11월 6일
편집: Adam Danz 2019년 11월 14일
"I'm trying to make a plot with 2 y-axes and wasn't able to use the yyaxis command since I want to change the uistack afterwards..."
There probably isn't a need to change the uistack. What exactly are you trying to do that involves the uistack? It would be cleaner to use the yyaxis function so if you could explain the problem you were having, maybe we could fix that instead of fixing the plan-b.
"Is there a way to link the axes to be able to "zoom" or change the parts visible without changing the axis values? "
It looks like the two datasets have different scales so you'll either need to
  1. Scale your data so the range of y values match between the two datasets, or
  2. don't link the axes but instead, carefully set the ylim() for each axis and the ytick.
To reiterate, I think the yyaxis solution is best and we could probably fix your uistack problem.
Alexander von Mach
Alexander von Mach 2019년 11월 7일
Thank you for your answer,
the problem I have with yyaxis right/left is, that the right axis is always at the top and I want the the plots of the left to be more visible since they're more important then the right one. I looked and it seems that this option isn't available in Matlab yet.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 11월 7일
편집: Adam Danz 2019년 11월 14일
Currently (r2019b) the UI stack cannot be controlled with yyaxis axes which always results in right-axis objects at the top of the stack. If left-axis objects need to be on top of right-axis objects, use plotyy() which is the outdated version of yyaxis (as of r2016a). Just note that plotyy() may be removed in future releases (by then, hopefully uistack options will be available for yyaxis).
Here's a demo
clf()
sph = subplot(2,1,1);
[ax,h1,h2] = plotyy(sph,[0 10 20 30],[30 20 10 0],[0 10 20 30],[0 20 0 20]);
% [ x1 ],[ y1 ],[ x2 ],[ y2 ]
set([h1,h2],'LineWidth',3) % set line object properties
ax(1).Color = 'None'; % set axis 1 (left axis) to transparent
uistack(ax(1),'top') % put axis 1 on top
  댓글 수: 5
Alexander von Mach
Alexander von Mach 2019년 11월 14일
Hello Adam,
sorry for the late answer. I see that it works with your example but when I use simple plot-lines then it seems that the right axis is always on top.
figure()
ax(1) = subplot(2,1,1);
yyaxis(ax(1),'left')
plot([0 10 20 30],[0 20 0 20],'LineWidth',2)
yyaxis(ax(1),'right')
plot([0 10 20 30],[30 20 10 0],'LineWidth',2)
title('Right axis on top')
ax(2) = subplot(2,1,2);
yyaxis(ax(2),'right')
plot([0 10 20 30],[30 20 10 0],'LineWidth',2)
yyaxis(ax(2),'left')
plot([0 10 20 30],[0 20 0 20],'LineWidth',2)
title('Left axis on top')
png.png
Adam Danz
Adam Danz 2019년 11월 14일
편집: Adam Danz 2019년 11월 14일
@Alexander von Mach, there was a problem with my original answer, sorry to have led you astray. I've updated my answer with recommendations to use plotyy() which is an outdated function but it's a lot easier and cleaner than overlaying and linking 2 axes on your own.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by