是否可以链接两个曲面图的轴以进行 3D 旋转



>假设我有两个大小相等的二维矩阵,并为每个矩阵创建一个曲面图。
有没有办法链接两个图的轴,以便可以在同一方向上同时 3D 旋转它们?

ActionPostCallbackActionPreCallback当然是一种解决方案,但可能不是最有效的解决方案。可以使用linkprop函数来同步相机位置属性。

linkprop([h(1) h(2)], 'CameraPosition'); %h is the axes handle

linkprop可以同步两个或多个axes(2D 或 3D)的任何图形属性。它可以看作是 linkaxes 函数的扩展,适用于 2D 图并仅同步axes限制。在这里,我们可以使用linkprop来同步相机位置属性, CameraPosition ,旋转axes时修改的那个。

这是一些代码

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

您可以使用以下图形属性列表

graph_props = fieldnames(get(gca));

一种方法是在旋转事件上注册回调,并在两个轴上同步新状态。

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

最新更新