MATLAB Answers

1

How do I link/lock multiple axis so that they all rotate simultaneously?

Asked by Fabian Gock on 6 Aug 2018
Latest activity Commented on by M Carpenter on 20 May 2019
Accepted Answer by Jan
I have two sets of axis in one figure and I want to use my mouse to 3D-rotate the plots. The Axis are aligned using linkaxes() and ax1.Position = ax2.Position
The problem is, that only one plot is rotating at a time.
I used two axis, because I want a different colormap on each one.
Thanks for any suggestions! -Fabian

  0 Comments

Sign in to comment.

Products


Release

R2017b

1 Answer

Answer by Jan
on 6 Aug 2018
 Accepted Answer

The 'Position' property is the location relative to the figure. But you want to control the 'CameraPosition', 'CameraTarget', 'CameraUpVector' and 'CameraViewAngle'. Use linkaxes to link them.

  3 Comments

I already called linkaxes.
See this example:
%create data
[X,Y,Z] = peaks(30);
Zprime = del2(Z);
contourmin = min(Z(:));
contourmax = max(Z(:));
pcolormin = min(Zprime(:));
pcolormax = max(Zprime(:));
%create figure and store handle
hF = figure;
%create axes for pcolor and store handle
hAxesP = axes;
%set colormap for pcolor axes
colormap(hAxesP,gray);
%plot pcolor for gradient
pcolorPlot = pcolor(X,Y,Zprime);
set(pcolorPlot,'FaceColor','interp','EdgeColor','interp');
%create color bar and set range for color
cbP = colorbar(hAxesP,'Location','west');
caxis(hAxesP,[pcolormin pcolormax]);
%create axes for the countourm axes
hAxesCM = axes;
%set visibility for axes to 'off' so it appears transparent
axis(hAxesCM,'off')
%set colormap for contourm axes
colormap(hAxesCM,cool);
%plot contourm
contourmPlot = contourm(X,Y,Z,20);
%create color bar and set range for color
cbCM = colorbar(hAxesCM,'Location','east');
caxis(hAxesCM,[contourmin contourmax]);
%link the two overlaying axes so they match at all times to remain accurate
linkaxes([hAxesP,hAxesCM]);
When I use my mouse to rotate the plots, only one is rotated (the one I grabbed)
I solved it with
linkprop([ax2 ax1], {'View', 'XLim', 'YLim', 'ZLim',})
Thanks :)
This was very helpful, thank you Fabian and Jan!

Sign in to comment.