MATLAB Answers

linkaxes of different stackedplots

24 views (last 30 days)
EK
EK on 30 Oct 2020
Edited: Adam Danz on 24 Nov 2020
Hi,
To display different signals with the same x-axis I used stackedplot. To compare different events of the signals I used subplot to display different stackplots side by side (in columns).
I would like to connect the axes, i.e. when I zoom or move a signal, this is done for all the signals of the other subplots as well.
If I want to use linkaxes, I get the error message: "There must be at least one valid axes."
Can anyone help me?
Here is the simplified code, which should run for everyone
X = [0:4:20];
Y = randi(100,6,3);
hAx(1) = subplot(1, 2, 1);
s = stackedplot(X,Y);
s.title('Plot 1');
hAx(2) = subplot(1, 2, 2);
s = stackedplot(X,Y);
s.title('Plot 2');
% linkaxes(hAx, 'x');
linkaxes([hAx(1) hAx(2)],'x')

  2 Comments

Adam Danz
Adam Danz on 30 Oct 2020
If you look at hAx(1) or hAx(2) after creating the two stackedplots you'll notice that they are handles to deleted axes. That's because the stackedplots replace the subplots.
stackedplot handles are not axes so you cannot use them with linkaxes.
linkprop([s1,s2],'XLimits') also didn't work, where s1 and s2 are the two stackedplot handles.
The XLimits property is not defined to be SetObservable so adding a listener would also not work.
Bummer....
EK
EK on 2 Nov 2020
oh crap, thanks for the help. Do you know a similar plot like Stackedplot, which allows you to zoom the subplots at the same time (for example with linkaxes)?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 19 Nov 2020
Edited: Adam Danz on 24 Nov 2020
I found a solution (weeks later) using the undocumented NodeChildren property to get the axis handles.
Instead of using the subplot command to position the stackedplot objects, you can specify their parent object as a panel, tab, tiledLayout, or gridLayout (see this answer).
fig = figure();
tlo = tiledlayout(fig,1,2);
nexttile(tlo);
s1 = stackedplot(rand(20,4),'Parent',tlo);
nexttile(tlo);
s2 = stackedplot(rand(20,4),'Parent',tlo);
% Get axes
ax1 = findobj(s1.NodeChildren, 'Type','Axes');
ax2 = findobj(s2.NodeChildren, 'Type','Axes');
linkaxes([ax1,ax2],'x')

  1 Comment

Seth Furman
Seth Furman on 20 Nov 2020
@EK, would it meet your use case to combine horizontally adjacent sub-plots from these side-by-side stacked plots?
For example
rng default
X = 0:4:20;
Y = randi(100,6,6);
stackedplot(array2table([X' Y]),{[1 4] [2 5] [3 6]},'XVariable',1);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!