Use linkaxes on where all x-axes are linked but only some y-axes are linked

15 views (last 30 days)
Hello,
I ran into a slight problem with linkaxes. I want to be able to use the zoom function on the axes flexibly.
I am plotting 4 subplots with the same x-axis limits (time) but different y-axis limits (temperature & humidity).
figure;
sb1 = subplot(4,1,1); plot([datetime('01.01.2000'):datetime('07.01.2000')],[26.2 25.1 25.3 25.6 25.2 25.4 24.1]); title('Temperature Room 1');
sb2 = subplot(4,1,2); plot([datetime('01.01.2000'):datetime('07.01.2000')],[26.2 24.9 24.3 24.5 24.4 24.4 24.1]); title('Temperature Room 2');
sb3 = subplot(4,1,3); plot([datetime('01.01.2000'):datetime('07.01.2000')],[39.5 38.9 35.6 26.1 25.6 24.1 23.5]); title('Humidity Room 1');
sb4 = subplot(4,1,4); plot([datetime('01.01.2000'):datetime('07.01.2000')],[39.5 35.5 29.4 25.2 24.8 28.2 23.5]); title('Humidity Room 2');
linkaxes([sb1,sb2,sb3,sb4],'x'); linkaxes([sb1,sb2],'y'); linkaxes([sb3,sb4],'y')
The axes are now only connected on the y-axis with each other, not on the x-axis anymore.
Is there an easy / common way to fix it that I am overlooking?
Thanks and Best Wishes!

Accepted Answer

the cyclist
the cyclist on 20 Jul 2023
I think this does what you intend. You need to link x & y simultaneously, and the order of calling linkaxes matters.
figure;
sb1 = subplot(4,1,1); plot([datetime('01.01.2000'):datetime('07.01.2000')],[26.2 25.1 25.3 25.6 25.2 25.4 24.1]); title('Temperature Room 1');
sb2 = subplot(4,1,2); plot([datetime('01.01.2000'):datetime('07.01.2000')],[26.2 24.9 24.3 24.5 24.4 24.4 24.1]); title('Temperature Room 2');
sb3 = subplot(4,1,3); plot([datetime('01.01.2000'):datetime('07.01.2000')],[39.5 38.9 35.6 26.1 25.6 24.1 23.5]); title('Humidity Room 1');
sb4 = subplot(4,1,4); plot([datetime('01.01.2000'):datetime('07.01.2000')],[39.5 35.5 29.4 25.2 24.8 28.2 23.5]); title('Humidity Room 2');
linkaxes([sb1,sb2],'xy'); linkaxes([sb3,sb4],'xy'); linkaxes([sb1,sb3],'x')
  2 Comments
Katy Weihrich
Katy Weihrich on 20 Jul 2023
Thank you! That is great!
Though, I am still a little confused why this version works.
When I try ...
linkaxes([sb1,sb2],'xy'); linkaxes([sb3,sb4],'xy'); linkaxes([sb1,sb2,sb3,sb4],'x')
... the link of the y axis for [sb1,sb2] and [sb3,sb4] gets overwritten.
I assume this is caused by linkaxes([sb1,sb3],'x') overwitting the sb1 & sb3 axes but the sb2 & sb4 axes linking to the sb1 & sb3 do not get overwitten?
the cyclist
the cyclist on 20 Jul 2023
I believe that what is happening in your version is that if you link sb1 and sb2 with
linkaxes([sb1,sb2],'x')
after linking with
linkaxes([sb1,sb2],'xy')
then that link supersedes the prior one, and removes the y-axes link between that pair of subplots.
The reason that mine worked as you intended is that I never replicated a link between any two sets of axes. I created the minimal set that would create all the links you wanted.
I believe instead you could have created links between each pair of subplots separately, and not relied on the "chain" of links that I effectively created. That would probably have been a more intuitive (if slightly less efficient) way to see what is going on.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!