How to show the plot at a specific height of a 3D plot?

13 views (last 30 days)
Hi There,
I have a 3D plot as follow:
x=0:10;
y=0:10;
[X,Y]=meshgrid(x,y);
z=X.^2+Y.^2;
surf(x,y,z)
How can I show the 2D plot (XY plot) at a specific height(z) such as z=100?
Thanks.

Answers (2)

Star Strider
Star Strider on 20 Nov 2015
You can combine the surf plot with a contour3 plot to overplot the specific contour at the level you want:
x=0:10;
y=0:10;
[X,Y]=meshgrid(x,y);
z=X.^2+Y.^2;
surf(X,Y,z) % Draw Surface
hold on
contour3(X,Y,z,[100 100], '-r', 'LineWidth',2) % Draw Contour At ‘100’
hold off

Walter Roberson
Walter Roberson on 20 Nov 2015
contour(x, y, z, [100 100])
Note that I had to use repeat the 100. If you were to call
contour(x, y, z, 100)
then that would mean that it should pick 100 different contour levels. The way that contour tells whether you are asking for a specific number of levels or if you are asking for a particular location for the contours is that it treats a scalar as being the number of contours and any vector as being the list of locations to place the contours at. So to get a contour at z = 100 we needed to make a vector out of the 100 by repeating it.
If you were asking about different levels such as 100 and 150 then you would not need to repeat them:
contour(x, y, z, [100 150])
  3 Comments
Walter Roberson
Walter Roberson on 13 Jul 2016
I do not understand the distinction between "the contour level specified by h" and "all the levels at height h" ? If you have region that rises from below h to above h, then contour(x, y, z, [h h]) will plot around it at the height that corresponds to h.
Kelly Kearney
Kelly Kearney on 13 Jul 2016
Do you mean you want to plot a contour plot, but instead of putting the contours at Z=0, you want them at a specified height? In newer versions of Matlab, you need to dive into some undocumented properties to do this:
[x,y,z] = peaks;
surf(x,y,z);
shading flat;
hold on;
h = 5;
[c,hc] = contour(x,y,z);
drawnow;
for ii = 1:length(hc.EdgePrims)
hc.EdgePrims(ii).VertexData(3,:) = h;
end
Alternatively, you could use the data in c with plot3 to create contour-like lines. Might be a more robust solution than above.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!