Multiple linkaxes calls on subplots?
Show older comments
Hi, I'm trying to link the plots in two figures and their subplots in a specific way.
I'm making two 1xN figures and want:
- All of the x axes to be linked.
- The y axis of the first subplot in figure 1 linked to the first subplot's y axis in figure 2.
- The y axis of the rest of the subplots(2-N) in both figures all linked.
figure
sp3=zeros(1,N);
for k = 1:N
sp3(k) = subplot(N,1,k);
plot(x,y(:,k));
title(strcat(Titles{k},{' Original'}));
xlabel('Time (s)');
ylabel('Voltage');
end
set(findall(gcf,'-property','FontSize'),'FontSize',12) ;
figure
sp4=zeros(1,N);
for k = 1:N
sp4(k) = subplot(N,1,k);
plot(x,y_edited(:,k));
title(strcat(Titles{k},{' Edited'}));
xlabel('Time (s)');
ylabel('Voltage');
end
set(findall(gcf,'-property','FontSize'),'FontSize',12) ;
linkaxes([sp3(1),sp4(1)],'y');
linkaxes([sp3(2:end),sp4(2:end)],'y');
linkaxes([sp3,sp4],'x');
Unfortunately, this isn't working, the y axes are not linking, this is particularly evident when zooming.
Any suggestions would be great! Thanks!
6 Comments
Adam
on 11 Jul 2017
Yes, you can usually use linkprop to get around limitations of only one linkaxes. Beware though you need to keep hold of the handle returned by linkprop
hLinks(1) = linkprop([sp3(1),sp4(1)],'YLim');
hLinks(2) = linkprop([sp3(2:end),sp4(2:end)],'YLim');
because otherwise once the link object goes out of scope the link itself will die off so you must store it somewhere where the lifetime is at least as long as that of the plots you want linked.
joey101
on 11 Jul 2017
Adam
on 11 Jul 2017
You'll get similar behaviour with linkaxes. Just manually over-ride the limits or swap the order of the axes if it is always the same way round causing the problem.
joey101
on 11 Jul 2017
Adam
on 11 Jul 2017
You must just be lucky because the help for linkaxes says:
'The first axes you supply to linkaxes determines the x- and y-limits for all linked axes'
Answers (2)
Elliot Claveau
on 21 Jun 2018
I found that using "linkprop" saves the properties over multiple call (as long as you define the variable "link1, link2..."). For example, I linked the Y axis of the top row, and the Y axis of the bottom row independently. With the third call, I was able to link all the X axis together, keeping the independent link between the Y axis.
ax{1,1} = subplot(2,2,1);
ax{1,2} = subplot(2,2,2);
ax{2,1} = subplot(2,2,3);
ax{2,2} = subplot(2,2,4);
link1 = linkprop([ax{1,1},ax{1,2}], 'YLim');
link2 = linkprop([ax{2,1},ax{2,2}], 'YLim');
link3 = linkprop([ax{1,1},ax{1,2},ax{2,1},ax{2,2}],'XLim');
1 Comment
Ziad Sliti
on 25 Feb 2022
That works for me !
Thank you for the tips, you have to define variable to keep multiple call of linkprop active.
Steven Lord
on 10 Jul 2017
0 votes
"Note: linkaxes is not designed to be transitive across multiple invocations. If you have three axes, ax1, ax2, and ax3 and want to link them together, call linkaxes with [ax1, ax2, ax3] as the first argument. Linking ax1 to ax2, then ax2 to ax3, "unbinds" the ax1-ax2 linkage."
3 Comments
joey101
on 11 Jul 2017
Jan
on 11 Jul 2017
How do you zoom in the axes? You can incluide Callbacks in the zoom() function, which can set the limits of the other axes accordingly. This is less convenient than linked properties, but there are no limitations in what you include in the callback function.
joey101
on 11 Jul 2017
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!