Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When doing


the result looks like this (with Mathematica

enter image description here

As you can easily see, at the "kink" where the function starts going above 0, Mathematica leaves a gap in the surface.

Why does it do that, and more importantly, what can I do about it?

share|improve this question
Increasing the number of PlotPoints seems to reduce the gap. – b.gatessucks Apr 27 '12 at 10:30
@b.gatessucks: True, but it also increases the memory needed, and doesn't really close the gap. And I've got other cases (with more complicated functions) where the gap gets much larger than in this example. Increasing the number of points isn't really practical in those cases. – celtschk Apr 27 '12 at 10:37
up vote 16 down vote accepted

The reason for this is automatic exclusion detection:

Here the discontinuity in the derivative is ugly:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> None]

Mathematica graphics

We can get Mathematica to auto-detect it, and compute the contours precisely, making the plot smooth around the discontinuity:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> Automatic]

Mathematica graphics

The side effect is a gap, as you noticed. Often one would prefer this gap to be filled with the same style as the plot. It can be accomplished by setting ExclusionsStyle, like this:

Plot3D[Max[p^2 - q^2, 0], {p, 0, 1}, {q, -1, 1}, 
 Exclusions -> Automatic, ExclusionsStyle -> Automatic]

Mathematica graphics

EDIT: As @celtschk notes, mesh lines or contour lines are not drawn inside the excluded region. This is very visible for large exclusions and can be prevented by forbidding exclusions with Exclusions -> None. Then the plot can be made smoother by increasing MaxRecursion and PlotPoints. Here's an example:

Plot3D[UnitStep[y], {x, -1, 1}, {y, -1, 1}, Exclusions -> None, MaxRecursion -> 5]

Mathematica graphics

share|improve this answer
Thank you, that gives exactly what I want. – celtschk Apr 27 '12 at 10:43
Correction: Almost exactly: The grid lines are not drawn in the filled gap, which gets very visible for large gaps. – celtschk Apr 27 '12 at 10:46
@celtschk You mean the mesh, right? (Mesh). That's true, the workaround would be Exclusions -> None and increasing MaxRecursion and PlotPloints manually until you get the right result. (I.e. the same thing you did.) – Szabolcs Apr 27 '12 at 10:47
Yeah, I meant the mesh. Exclusions->None plus sufficiently high MaxRecursion indeed did the trick. Thank you again. – celtschk Apr 27 '12 at 11:02
@celtschk The refinement in Mma's adaptive plotting is based on the curvature, so it'll converge fast around a discontinuity of the derivative (where the curvature is infinite). MaxRecursion just controls the number of allowed recursive refinement steps. – Szabolcs Apr 27 '12 at 11:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.