【问题标题】:Is it possible to link the axes of two surface plots for 3d-rotation?是否可以链接两个曲面图的轴以进行 3d 旋转?
【发布时间】:2013-09-12 12:51:29
【问题描述】:

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

【问题讨论】:

  • surf(A); hold on; surf(B);?不过,这会将两个图都添加到相同的坐标系中。你想要 2 帧吗?
  • 是的,两帧会更好..
  • 有一个简化了@umlum 的回调解决方案的单行代码。

标签: matlab 3d plot axes


【解决方案1】:

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

【讨论】:

  • 哦,linkprop() 确实不错。此解决方案还提供持续更新,而我的答案仅在用户抬起鼠标按钮后同步。
  • 这成功了!但我发现linkprop([h(1) h(2)], 'View');'CameraPosition' 效果更好。
  • @ESala,感谢您的提示;但是,ViewCameraPosition 好多少? CameraPosition 不够健壮吗?
  • 我发现View 感觉更一致。对于CameraPosition,当您与某个坐标轴成小角度时,会发生某种捕捉。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2021-09-23
    • 2021-12-11
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多