Matlab how to change contourf plot's location

2019-01-25 12:19发布

问题:

I have a 3d surface in my figure surf(x,y,z)

I also have a contourf surface (which is basically a 2D plane).

I plot them in the same figure but the contourf plot is automatically at z=0 level. I want to move the contourf plot to z=-10 (or any value on z-axis) but I can't do it.

I am sure it is easy but I can't find the answer in MATLAB help/Google. Any ideas?

回答1:

Consider the following example:

%# plot surface and contour
Z = peaks;
surf(Z), hold on
[~,h] = contourf(Z);       %# get handle to contourgroup object

%# change the ZData property of the inner patches
hh = get(h,'Children');    %# get handles to patch objects
for i=1:numel(hh)
    zdata = ones(size( get(hh(i),'XData') ));
    set(hh(i), 'ZData',-10*zdata)
end


UPDATE:

The above doesn't work anymore in HG2. It can be fixed using a hidden property of contours ContourZLevel:

Z = peaks;
surf(Z), hold on
[~,h] = contourf(Z);
h.ContourZLevel = -10;

You can also use hgtransform to achieve a similar thing, which is the documented and recommended approach.

See another answer of mine for further explanation: plot multiple 2d contour plots in one 3d figure.