Pregunta

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?

¿Fue útil?

Solución

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));

Otros consejos

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top