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 件のコメント

joey101
joey101 2017 年 7 月 11 日
編集済み: joey101 2017 年 7 月 11 日
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.
Adam
Adam 2017 年 7 月 11 日
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 2017 年 7 月 11 日
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 2017 年 7 月 11 日
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 2017 年 7 月 11 日
Nah linkaxes has always extended all of the limits to the largest of the axes linked.
Adam
Adam 2017 年 7 月 11 日
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'

サインインしてコメントする。

回答 (2 件)

Elliot Claveau
Elliot Claveau 2018 年 6 月 21 日

2 投票

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 件のコメント

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

サインインしてコメントする。

Steven Lord
Steven Lord 2017 年 7 月 10 日

0 投票

"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 件のコメント

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

質問済み:

2017 年 7 月 10 日

コメント済み:

2022 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by