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?
Is it possible to link the axes of two surface plots for 3d-rotation?
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));
Oh linkprop() is nice indeed. This solution also gives continous updates while my answer only synchronizes once the user lifts the mouse button. –
Masha
This worked! but I find that
linkprop([h(1) h(2)], 'View');
works even better than 'CameraPosition'
. –
Salvadorsalvadore @ESala, thanks for the tip; however, how is
View
better than CameraPosition
? Is CameraPosition
not as robust? –
Brewer 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. –
Heer 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
© 2022 - 2024 — McMap. All rights reserved.
surf(A); hold on; surf(B);
? This will add both plots to the same coordinate frame though. You want to have 2 frames? – Masha