简体   繁体   中英

Contour cut of a 3d graph in Matlab

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

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

在此输入图像描述

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)

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)

例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM