Multiple linkaxes calls on subplots?

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 Kommentare

joey101
joey101 am 11 Jul. 2017
Bearbeitet: joey101 am 11 Jul. 2017
I think I got it to work with this:
linkaxes([sp3,sp4],'x');
linkprop([sp3(1),sp4(1)],'YLim');
linkprop([sp3(2:end),sp4(2:end)],'YLim');
But then I notice that the y axis limits are automatically becoming what was set in the first plot's limits, so if the next plots are outside of this range they look terrible.
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
joey101 am 11 Jul. 2017
Thanks, that makes sense, but then I notice that the y axis limits are automatically becoming what was set in the first plot's limits, so if the next plots are outside of this range they look terrible.
Adam
Adam am 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
joey101 am 11 Jul. 2017
Nah linkaxes has always extended all of the limits to the largest of the axes linked.
Adam
Adam am 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'

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Elliot Claveau
Elliot Claveau am 21 Jun. 2018

2 Stimmen

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 Kommentar

Ziad Sliti
Ziad Sliti am 25 Feb. 2022
That works for me !
Thank you for the tips, you have to define variable to keep multiple call of linkprop active.

Melden Sie sich an, um zu kommentieren.

Steven Lord
Steven Lord am 10 Jul. 2017

0 Stimmen

"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 Kommentare

joey101
joey101 am 11 Jul. 2017
Ahh I see, any known workarounds for my desired functionality?
Jan
Jan am 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
joey101 am 11 Jul. 2017
visually with my mouse

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Jul. 2017

Kommentiert:

am 25 Feb. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by