在一个3D图中绘制多个2D轮廓图[MATLAB]

8
我想知道如何在 3D 图中绘制多个间隔在 z 轴上的 2D 等高线图,就像这样的图示所示:

blah


你可能正在寻找的命令是contourslice。请参阅文档以获取更多信息。 - CitizenInsane
@CitizenInsane:那个函数是用来可视化体积数据的,我不认为OP在这里想要做那个。 - Amro
@Amro Humm 或许太容易想到数据是体积数据了。你的解决方案很好。 - CitizenInsane
1个回答

10

注意:本答案的第一部分适用于HG1图形。如果您正在使用MATLAB R2014b及更高版本(HG2),请参见第二部分。


HG1:

contour函数内部会创建多个patch对象,并将它们作为一个组合的hggroup对象返回。因此,我们可以通过在Z维度上移位到所需的水平(默认情况下,等高线显示在z=0)来设置所有补丁的ZData

这是一个例子:

[X,Y,Z] = peaks;
surf(X, Y, Z), hold on       % plot surface

[~,h] = contour(X,Y,Z,20);   % plot contour at the bottom
set_contour_z_level(h, -9)
[~,h] = contour(X,Y,Z,20);   % plot contour at the top
set_contour_z_level(h, +9)

hold off
view(3); axis vis3d; grid on

多个轮廓图

这是上面使用的set_contour_z_level函数的代码:

function set_contour_z_level(h, zlevel)
    % check that we got the correct kind of graphics handle
    assert(isa(handle(h), 'specgraph.contourgroup'), ...
        'Expecting a handle returned by contour/contour3');
    assert(isscalar(zlevel));

    % handle encapsulates a bunch of child patch objects
    % with ZData set to empty matrix
    hh = get(h, 'Children');
    for i=1:numel(hh)
        ZData = get(hh(i), 'XData');   % get matrix shape
        ZData(:) = zlevel;             % fill it with constant Z value
        set(hh(i), 'ZData',ZData);     % update patch
    end
end

HG2:

从R2014b开始,上述解决方案已不再适用。在HG2中,等高线对象不再有任何图形对象作为子级 (为什么某些对象的Children属性为空?)。

幸运的是,等高线的一个隐藏属性叫做ContourZLevel,可以轻松修复。您可以在此处了解更多这里关于未记录的等高线绘图自定义。

所以先前的示例变得非常简单:

[X,Y,Z] = peaks;
surf(X, Y, Z), hold on       % plot surface

[~,h] = contour(X,Y,Z,20);   % plot contour at the bottom
h.ContourZLevel = -9;
[~,h] = contour(X,Y,Z,20);   % plot contour at the top
h.ContourZLevel = +9;

hold off
view(3); axis vis3d; grid on

轮廓


另一种适用于所有版本的解决方案是将轮廓“父级化”到hgtransform对象中,并使用简单的z-平移进行变换。代码如下:

t = hgtransform('Parent',gca);
[~,h] = contour(X,Y,Z,20, 'Parent',t);
set(t, 'Matrix',makehgtform('translate',[0 0 9]));

谢谢!这正是我想绘制的图形类型。有没有办法调整间距/视角,使轮廓不重叠?(我正在绘制3个轮廓级别,就像图表一样,但是每个轮廓都与当前透视图重叠) - Krish
您可以通过交互式旋转在三维视图中旋转视角,或者使用view(az,el)进行编程控制:http://www.mathworks.com/help/matlab/visualize/setting-the-viewpoint-with-azimuth-and-elevation.html。您还可以[控制相机](http://www.mathworks.com/help/matlab/visualize/view-control-with-the-camera-toolbar.html),以获得更大的灵活性。 - Amro
1
我已经更新了答案,展示了HG1和HG2的解决方案。 - Amro

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