Clear Filters
Clear Filters

linkaxes with different x scales

21 views (last 30 days)
Mech Princess
Mech Princess on 13 Feb 2015
Commented: Adam Danz on 15 Apr 2021
Hi, I have 2 plots with same y axis but different x axis. But the two x axis are propotional. How do I link x=1 of plot 1 to x=10 of plot 2 and x=10 to x=100. and also for both plots to zoom in and out together like linkaxes. See below. Thanks
y = rand(10,1);
x1 = 1:10;
x2 = 10:10:100;
ax(1) = subplot(211); plot(x1,y)
ax(2) = subplot(212); plot(x2,y)

Answers (3)

dario on 12 May 2015
Find attached a simple demo :)
  1 Comment
Adam Danz
Adam Danz on 15 Apr 2021
Nice demo, dario. It works well when panning and applying other translations of the axes but it does not work when the user zooms in/out of the axes. The solution does not maintain the relative aspect ratios between the two pairs of axes which results in the misalignment between the green curve and the underlying red curve.
I recently addressed this same question in another thread. This solution correctly yokes the two pairs of axis limits:

Sign in to comment.

Peter Kövesdi
Peter Kövesdi on 2 Apr 2019
Edited: Peter Kövesdi on 2 Apr 2019
Adams solution works well, but fails on 'Restore View' from the context menu in zoom or pan mode.
To get the axes synchronized on 'Restore View' as well, add the same callback function also to the zoom and the pan objects:
h = zoom;
h.ActionPostCallback = @(src,evt) updateAxes( ax(1), ax(2) );
h = pan;
h.ActionPostCallback = @(src,evt) updateAxes( ax(1), ax(2) );
But then, if only added there, it would synchronize only when releasing the mouse button, not while dragging, so both is recommended, addlistener 'PostSet' and ActionPostCallback.

Adam on 13 Feb 2015
xLimListener = addlistener( hAxes1, 'XLim', 'PostSet', @(src,evt) disp( 'X Changed' ) )
yLimListener = addlistener( hAxes1, 'YLim', 'PostSet', @(src,evt) disp( 'Y Changed' ) )
will respond whenever the limits of an axes change.
So you can just create your own callback to go in-place of that one which will change the XLim and YLim properties of the second axes in a manner of your choosing relative to the XLim and YLim of the first axes.
Then every time the first axes are zoomed, the second axes will also be zoomed.
Mech Princess
Mech Princess on 13 Feb 2015
Thanks for the reply. I modified the code as following. Zooming together works but the x-axis is wrong; both axis have 1 to 10. first plot should be 1 to 10 and second 10 to 100. when I zoom 1 in first plot it should zoom to 10 in second plot.
clear; close all; clc;
ax(1)=subplot(211); plot(y);
ax(2)=subplot(212); plot(y);
xLimListener = addlistener( ax, 'XLim', 'PostSet', @(src,evt) disp( 'X Changed' ) )
yLimListener = addlistener( ax, 'YLim', 'PostSet', @(src,evt) disp( 'Y Changed' ) )
Adam on 14 Feb 2015
You can't use linkaxes. You have to program your own callback to change the XLim and YLim properties of the second axes, something like:
function updateAxes( axes1, axes2 )
xLim1 = get( axes1, 'XLim' );
yLim1 = get( axes1, 'YLim' );
set( axes2, 'XLim', 10 * xLim1 );
set( axes2, 'YLim', 10 * yLim1 );
then plug that function into those addlistener calls in place of my disp function.
I can never remember the syntax for callbacks so I always just try it out, but something like:
xLimListener = addlistener( ax(1), 'XLim', 'PostSet', @(src,evt) updateAxes( ax(1), ax(2) );
You can probably use the src as the first axes since that is what it should point to, but I haven't tested out that syntax so you'll have to work out exactly what it needs to be.

Sign in to comment.


Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!