在一个三维图中绘制多个2d等高线图[Matlab]

时间:2014-07-07 23:19:46

标签: matlab plot matlab-figure contour

我想知道如何绘制在z轴上间隔开的多个2D等高线图,如下所示:

blah

1 个答案:

答案 0 :(得分:7)

  

注意:这个答案的第一部分是针对HG1图形的。如果您正在使用MATLAB R2014b及更高版本(HG2),请参阅第二部分。


HG1:

contour函数在内部创建了许多patch个对象,并将它们作为组合hggroup object返回。因此,我们可以通过将Z维度移动到所需级别来设置所有补丁的ZData(默认情况下,轮廓显示在z = 0)。

以下是一个例子:

[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

multiple_contour_plots

以下是上面使用的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中,contour objects不再有任何图形对象作为子项(Why Is the Children Property Empty for Some Objects?)。

幸运的是,有一个简单的修复方法,其中隐藏属性称为ContourZLevel。 您可以了解更多未记录的轮廓图herehere自定义。

所以前面的例子就变成了:

[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

contours


适用于所有版本的另一个解决方案是将轮廓“父”加到hgtransform对象,并使用简单的z-translation对其进行转换。像这样:

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