Sabrina
Sabrina

Reputation: 11

Contour cut of a 3d graph in Matlab

How can I cut off a 3D graph in Matlab at a particular contour set (e.g., the graph x^2+y^2 shall have a clear upper edge like a glass)? Thanks!

Upvotes: 1

Views: 2276

Answers (2)

marsei
marsei

Reputation: 7751

Two methods you may use for slicing a 3-D plot.

  • (left) setting NaN above and below two thresholds (@Molly's proposal)
  • (right) using the ZLim property of the current axes, using the exact same thresholds.

You can see that NaN respects the tiles created with surf, so it works at the surf level. On the other hand, ZLim creates nicely cut graph, even through the tiles - it works at the rendering level.

Finally, to have a better cut using NaN, you can define a finer grid (meshgrid(-10:0.01:10, -10:0.01:10) for example) but you will still depend on the mesh created. In addition, the ZLim method will only easily work for slicing at constant z (but this what a contour is about).

The following plot

enter image description here

is produced by

[x y] = meshgrid(-10:10,-10:10);
z = x.^2+y.^2;
figure

%%%% solution 1 (NaN)
z_trim = z;
z_trim(z_trim<100) = NaN;
z_trim(z_trim>150) = NaN;

subplot(1,2,1);
surf(z_trim)
set(gca, 'Visible', 'off');
view(-20,30)


%%%% solution 2 (ZLim)
subplot(1,2,2);
surf(z)
set(gca,'Zlim',[100 150], 'Visible', 'off');
view(-20,30)

Upvotes: 3

Molly
Molly

Reputation: 13610

You can set values above the cutoff to nan:

[X,Y] = meshgrid(-100:100,-100:100);
Z = X.^2+Y.^2;
ind = Z > 10000;
Z(ind) = nan;
mesh(X,Y,Z)

example

Upvotes: 1

Related Questions