9

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?

Amro
  • 123,847
  • 25
  • 243
  • 454
theenemy
  • 345
  • 2
  • 4
  • 9

1 Answers1

14

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

screenshot


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.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • if you hate loops, you can rewrite the last part in one-line: `set(hh, {'ZData'}, cellfun(@(x) -10*ones(size(x)), get(hh,{'XData'}), 'UniformOutput',false))` – Amro Nov 08 '11 at 18:48
  • excellent. thanks a lot. I just have to learn more about objects and how to use get and set to "inspect" and "change" stuff to my liking. do you know any good reference/tutorial other than Matlab Help ? – theenemy Nov 08 '11 at 19:10
  • @Amro nice one. This will come in handy for sure. – John Colby Nov 10 '11 at 07:58