Synchronize axes limits with direct relation

Hi,
I would like to create a figure with two axes (left and right). Suppose on the left axis is Celsius, on the right Fahrenheit. Between those two units is always a direct relation. So whenever I change the left axis limits with ylim([a b]) I would like to see the limits on the right axis limits change accordingly/automatically. I have seen the option to link axes, however that seem work only if axes have the same limits. Any suggestion how I might tackle this problem?

댓글 수: 1

jonas
jonas 2018년 10월 6일
I think you need to write a callback for this. Defining callbacks is one of the things I want to learn, so I might use this as an example and report back if I am successful :)

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

 채택된 답변

Erikve
Erikve 2018년 10월 7일

0 개 추천

Hi Jonas,
Thanks for you help. Actually your suggestions gave me the right push in the direction to make it work :). I used an "addlistener" in my function, and this way I added two listeners for each axes, which are activated whenever Y limits of either left or right axes are changed (for example from command line).
function test_limits()
figure;
%%Left axes
ax(1) = axes('color','none');
%%Passive right axes, should update with left
ax(2) = axes('color','none','yaxislocation','right','xcolor','none');
%%set initial axes Limits
ax(1).YLim = [0 1];
ax(2).YLim = [0 2];
%%set figure callback
addlistener(ax(1), 'YLim','PostSet', @(~,~)(adjust_right_limits(ax)));
addlistener(ax(2), 'YLim','PostSet', @(~,~)(adjust_left_limits(ax)));
%%plot axes
axes(ax(1))
end
%%Executes when left/right limits have been changed
function adjust_right_limits(ax)
ax(2).YLim = ax(1).YLim*2;
end
function adjust_left_limits(ax)
ax(1).YLim = ax(2).YLim/2;
end

댓글 수: 1

jonas
jonas 2018년 10월 7일
편집: jonas 2018년 10월 7일
Nice! listener was what I was looking for too. I knew there had to be something like that available :)

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

추가 답변 (1개)

jonas
jonas 2018년 10월 7일

0 개 추천

This is the best I could do. I could not find a way to execute the callback when updating the axes limits from the command window, perhaps someone more knowledgeable can chime in here as I feel this should be possible. This script will update the 2nd yaxis upon pressing the figure window.
figure;
%%Left axes
ax(1) = axes('color','none');
%%Passive right axes, should update with left
ax(2) = axes('color','none','yaxislocation','right','xcolor','none');
%%set figure callback
set(gcf,'WindowButtonDownFcn',{@FigCallback,ax})
axes(ax(1))
%%Executes when clicking the figure window
function FigCallback(scr,evnt,ax,~)
set(ax(2),'ylim',((ax(1).YLim.*(9/5)) + 32))
axes(ax(1))
end

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

질문:

2018년 10월 6일

편집:

2018년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by