Use linkaxes on where all x-axes are linked but only some y-axes are linked
39 views (last 30 days)
Show older comments
Katy Weihrich
on 19 Jul 2023
Commented: the cyclist
on 20 Jul 2023
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!
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Graphics Object Properties 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!