How can I bring patch in front of a surface plot?

12 views (last 30 days)
Hi!
I am trying to plot a patch on top of a surface plot, but it always go 'behind'. Is there any way to bring it to the front?
figure;
s = surface(m.z, P, A,'edgecolor', 'none')
hold on;
patch([0,0,5.04,5.04], [0, 60, 60, 0], 'red', 'FaceAlpha',.3)
I need to shade an area of the surface plot, so I'd like to have it on top of it. Following you see a simpler but representative example of the problem I am facing, the patch is hidden (see the bottom left corner):
Thank you in advance.

Accepted Answer

Star Strider
Star Strider on 26 Jun 2020
The patch call does not define the ‘Z’ level, so by default it is zero. Change that to put it where you want it (this puts it at 10):
zlvl = 10;
patch([0,0,5.04,5.04], [0, 60, 60, 0], ones(1,4)*zlvl, 'red', 'FaceAlpha',.3)
To illustrate:
x = -1:0.1:1;
[X,Y] = ndgrid(x);
Z = X.^2 - Y.^3;
figure
surf(X, Y, Z)
hold on
patch([0 0 0.5 0.5], [0 1 1 0], 'r')
patch([0 0 0.5 0.5]-0.5, [0 1 1 0], ones(1,4)*1.5, 'g')
hold off
grid on
.
  2 Comments
gpr
gpr on 26 Jun 2020
that's cool, thanks. In that case, I am wondering: what if I want to shade z>10? with your method you define the maximum z, what if I want it to appear for z>10?
Star Strider
Star Strider on 26 Jun 2020
As always, my pleasure!
Define ‘zlvl’ to be whatever you want.
One option is:
zlvl = max(A(:));
to have it above everything else (assuming we are looking at the surface you are plotting from the top down). Define it similarly for other options.
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!