Say I have two 2-dimensional matrices of equal size and create a surface plot for each of them.
Is there a way to link the axes of both plots, so that one can 3D-rotate both of them simultaneously in the same direction?
Asked
Active
Viewed 6,216 times
9

bluebox
- 555
- 1
- 8
- 19
-
`surf(A); hold on; surf(B);`? This will add both plots to the same coordinate frame though. You want to have 2 frames? – umlum Sep 12 '13 at 13:09
-
Yes, two frames would be better.. – bluebox Sep 12 '13 at 13:14
-
1There is a one-liner that simplifies @umlum 's callback solution. – marsei Sep 12 '13 at 14:56
2 Answers
14
Playing with ActionPostCallback
and ActionPreCallback
is certainly a solution, but probably not the most efficient one. One may use linkprop
function to synchronize the camera position property.
linkprop([h(1) h(2)], 'CameraPosition'); %h is the axes handle
linkprop
can synchronize any of graphical properties of two or more axes
(2D or 3D). It can be seen as an extension of the linkaxes
function that works for 2D plots and synchronizes the axes
limits only. Here, we can use linkprop
to synchronize the camera position property, CameraPosition
, the one that is modify when one rotate an axes
.
Here is some code
% DATA
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z1 = sin(R)./R;
Z2 = sin(R);
% FIGURE
figure;
hax(1) = subplot(1,2,1); %give the first axes a handle
surf(Z1);
hax(2) = subplot(1,2,2); %give the second axes a handle
surf(Z2)
% synchronize the camera position
linkprop(hax, 'CameraPosition');
You can have a list of the graphical properties with
graph_props = fieldnames(get(gca));

marsei
- 7,691
- 3
- 32
- 41
-
Oh linkprop() is nice indeed. This solution also gives continous updates while my answer only synchronizes once the user lifts the mouse button. – umlum Sep 12 '13 at 16:10
-
1This worked! but I find that `linkprop([h(1) h(2)], 'View');` works even better than `'CameraPosition'`. – ESala Oct 27 '15 at 14:11
-
@ESala, thanks for the tip; however, how is `View` better than `CameraPosition`? Is `CameraPosition` not as robust? – chessofnerd Apr 26 '16 at 16:56
-
I found that `View` feels more consistent. With `CameraPosition` there is some kind of snapping going on when you're at a small angle to one of the coordinate axes. – Erik Mar 03 '17 at 12:43
5
One way is to register a callback on rotation events and synchronize the new state across both axes.
function syncPlots(A, B)
% A and B are two matrices that will be passed to surf()
s1 = subplot(1, 2, 1);
surf(A);
r1 = rotate3d;
s2 = subplot(1, 2, 2);
surf(B);
r2 = rotate3d;
function sync_callback(~, evd)
% Get view property of the plot that changed
newView = get(evd.Axes,'View');
% Synchronize View property of both plots
set(s1, 'View', newView);
set(s2, 'View', newView);
end
% Register Callbacks
set(r1,'ActionPostCallback',@sync_callback);
set(r1,'ActionPreCallback',@sync_callback);
set(r2,'ActionPostCallback',@sync_callback);
set(r2,'ActionPreCallback',@sync_callback);
end

umlum
- 1,107
- 6
- 11