# How can I zoom in/out in a plot with two x axes?

9 views (last 30 days)
Amy Lg on 14 Apr 2022
Commented: Amy Lg on 26 May 2022
I am plotting a figure with two x axis, in which the top x axis is a parameter equivalent to bottom x axis based on a relation. I appreciate if someone could help me to correct my code to be able to do zoom in/out on figure and all axes at the same time?
t = 0:0.05:10;
S = cos(t);
figure; plot(t, abs(S), 'r');
box off
xlabel('Time (ns)')
ylabel('Amplitude')
Tm = (t+10)./(0.0032*2*pi); %top x axis
%--------------------------------------------------------------------------
hAx = gca;
hAx = axes('Position',hAx.Position,'XAxisLocation','top','YAxisLocation','right','color','none');
%--------------------------------------------------------------------------
hAx.XLim = [Tm(1) Tm(end)];
hAx.XTick = linspace(Tm(1),Tm(end),11);
hAx.XColor = 'black';
hAx.XLabel.String = 'Equivalent Param.';
hAx.XLabel.FontSize = 10;
%--------------------------------------------------------------------------
hAx.YColor = 'black';
hAx.YTickLabel = ' ';

Voss on 15 Apr 2022
If the XLimits of each axes were supposed to always be the same as each other as you zoom, then I would say you could use linkaxes, but here the situation is that the XLimits of one axes are related to the XLimits of the other axes by the equation Tm = (t+10)./(0.0032*2*pi);
In this case, I would write a function that does the updating of one axes' XLimits based on the value of the other axes' XLimits. Then set that function to execute when zooming happens, by setting the ActionPostCallback property of the zoom object in the figure. (The function would have to have access to both axes.) Something like this:
t = 0:0.05:10;
S = cos(t);
figure; plot(t, abs(S), 'r');
box off
xlabel('Time (ns)')
ylabel('Amplitude')
Tm = (t+10)./(0.0032*2*pi); %top x axis
%--------------------------------------------------------------------------
hAx_bottom = gca;
hAx = axes('Position',hAx_bottom.Position,'XAxisLocation','top','YAxisLocation','right','color','none');
%--------------------------------------------------------------------------
hAx.XLim = [Tm(1) Tm(end)];
% hAx.XTick = linspace(Tm(1),Tm(end),11);
hAx.XColor = 'black';
hAx.XLabel.String = 'Equivalent Param.';
hAx.XLabel.FontSize = 10;
%--------------------------------------------------------------------------
hAx.YColor = 'black';
hAx.YTickLabel = ' ';
% store the axes' handles in a struct associated with
% the figure so the cb_zoom function can access them:
guidata(gcf(),struct('hAx',hAx,'hAx_bottom',hAx_bottom));
% associate cb_zoom with the zoom object:
set(zoom(),'ActionPostCallback',@cb_zoom);
% this function is executed when zooming occurs
function cb_zoom(f,evt)
% Tm = (t+10)./(0.0032*2*pi);
% t <-> hAx_bottom
% Tm <-> hAx (top axes)
S = guidata(f);
if evt.Axes == S.hAx % top axes is being zoomed on
% update the bottom axes XLimits
set(S.hAx_bottom,'XLim',get(S.hAx,'XLim')*0.0032*2*pi-10); % Tm -> t
else % bottom axes is being zoomed on
% update the top axes XLimits
set(S.hAx,'XLim',(get(S.hAx_bottom,'XLim')+10)./(0.0032*2*pi)); % t -> Tm
end
end
##### 1 CommentShowHide None
Amy Lg on 26 May 2022
Thank you so much!

### More Answers (1)

Bruno Luong on 15 Apr 2022
hAx1 = gca;
hAx = axes('Position',hAx1.Position,'XAxisLocation','top','YAxisLocation','right','color','none');
...