能否将两个表面图的轴连接起来以进行3D旋转?

9

假设我有两个相同大小的二维矩阵,并为每个矩阵创建一个表面图。
是否有一种方法可以链接两个图的轴,使得可以同时以相同方向进行三维旋转?


surf(A); hold on; surf(B);?这将把两个图形添加到同一个坐标系中。您想要有两个坐标系吗? - umlum
是的,两个框架会更好。 - bluebox
1
有一个一行代码可以简化 @umlum 的回调解决方案。 - marsei
2个回答

14

使用linkprop函数可以同步相机位置属性,这是一种解决方案,但可能不是最高效的方法,与使用ActionPostCallbackActionPreCallback相比。

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

哦,linkprop()确实很好。这个解决方案还可以在用户松开鼠标按钮之前持续更新,而我的答案只能同步一次。 - umlum
1
这个方法可行!但我发现 linkprop([h(1) h(2)], 'View');'CameraPosition' 更好用。 - ESala
@ESala,感谢您的提示;但是,ViewCameraPosition更好在哪里?CameraPosition不够强大吗? - chessofnerd
我发现View更加一致。使用CameraPosition时,当您与其中一个坐标轴的角度很小时,会出现某种捕捉效果。 - Erik

5
一种方法是在旋转事件上注册回调,并在两个轴上同步新状态。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接