在一个3d图形中绘制多个2d等高线图[Matlab] [英] plot multiple 2d contour plots in one 3d figure [Matlab]

查看:102
本文介绍了在一个3d图形中绘制多个2d等高线图[Matlab]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在这样的3D图形中绘制在z轴上隔开的多个2D等高线图:

I would like to know how to plot multiple, 2D contour plots spaced apart in the z-axis, in a 3D figure like this:

推荐答案

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

NOTE: The first part of this answer was meant for HG1 graphics. See the second part if you're working with MATLAB R2014b and up (HG2).


HG1:

contour 函数在内部创建多个 patch 对象,并将它们作为组合的


HG1:

The contour function internally creates a number of patch objects, and returns them as a combined hggroup object. So we could set the ZData of all patches by shifting in the Z-dimensions to the desired level (by default contour is shown at 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

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

Here is the code for the set_contour_z_level function used above:

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中,轮廓对象不再具有任何图形对象作为子对象(为什么是儿童属性对于某些对象而言是空的?).


HG2:

The above solution doesn't work anymore starting with R2014b. In HG2, contour objects no longer have any graphic objects as children (Why Is the Children Property Empty for Some Objects?).

幸运的是,有一个简单的修复方法,其轮廓的 hidden 属性称为ContourZLevel. 您可以在此处此处.

Fortunately there is an easy fix, with a hidden property of contours called ContourZLevel. You can learn more undocumented customizations of contours plots here and here.

因此,前面的示例变成了:

So the previous example simply becomes:

[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-translation进行转换.像这样:

Another solution that works in all versions would be to "parent" the contour to a hgtransform object, and transform that using a simple z-translation. Something like this:

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

这篇关于在一个3d图形中绘制多个2d等高线图[Matlab]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆