# Plot3D with a single contour highlighted. Different styles above and below

Here is a simple example that gives spurious results.

Plot3D[-Sqrt[x^2 + y^2], {x, -5, 5}, {y, -5, 5},
BoxRatios -> {1, 1, 1} , Mesh -> None,
ColorFunction ->
Function[{x, y, z},
If[z <= .5, Blue, Directive[White, Opacity[0.4]]]]]

1. Why is the blue-white "boundary" somewhere around a value of -3.5, not 0.5 as specified in the ColorFunction?

2. Why is the boundary so rough, the boundary should very obviously be a circle for this simple example?

3. How can fix these problems and draw a clean smooth contour at the interface?

EDIT How can I draw a single meshline at a desired height? For example:

Plot3D[-Sqrt[x^2 + y^2], {x, -5, 5}, {y, -5, 5},
BoxRatios -> {1, 1, 1} , MeshFunctions -> {#3 &}, Mesh -> 1,
MeshStyle -> Directive[Magenta, Thickness[0.02]],
ColorFunction ->
Function[{x, y, z},
If[z <= .6, Blue, Directive[White, Opacity[0.4]]]],PlotPoints->300]

does almost exactly what I want, except that I have no way to control the placement of this 1 Meshline. How can I control its height value?

This should be enough, as I can easily obscure the bumpy boundary with this clean meshline.

• have you tried increasing the number of plotted points? For example adding the option PlotPoints -> 100 or higher. With 400 I get a decently smooth separation
– glS
Commented Mar 25, 2017 at 23:36
• Yes, that improves it somewhat, but it never converges to a nice circle that we know it (is in this case). Ultimately, I need to extend the solution here to a ListPlot3D which comes from data I can't access analytically, and I want it to smooth over those tiny adjustments and just made a nice smooth thick line. (200 PlotPoints i.imgur.com/Ckvfpsy.png). Commented Mar 25, 2017 at 23:40
• Yes, I agree that 400 plotpoints is roughly satisfactory in terms of smoothness, but my real problem involves a ListPlot3D of data that is taxing to generate and I will not be able to call for it on such a fine mesh. Surely there is a more sophisticated approach? Also, this doesn't address why the boundary appears where it does. Commented Mar 25, 2017 at 23:43

Plot3D[-Sqrt[x^2 + y^2], {x, -5, 5}, {y, -5, 5}, BoxRatios -> 1,
MeshStyle -> Directive[Thick, Red],
MeshFunctions -> {#3 &}, Mesh -> {{-3}},

Alternatively, break the plotted surface into two parts and use PlotStyle to color the two parts and BoundaryStyle to color the border between the two pieces:

Plot3D[{ConditionalExpression[-Sqrt[x^2 + y^2], -Sqrt[x^2 + y^2] <= -3.],
ConditionalExpression[-Sqrt[x^2 + y^2], -Sqrt[x^2 + y^2] >= -3.]},
{x, -5, 5}, {y, -5, 5},
BoundaryStyle -> {1 -> None, 2 -> Directive[Thick, Red]},
BoxRatios -> 1, Mesh -> None,
PlotStyle -> {Blue, Opacity[0.4, White]}] /. Line -> (Tube[#, .1] &)

Update: Is there a good way to get the x,y meshlines back without disrupting this solution?

Plot3D[-Sqrt[x^2 + y^2], {x, -5, 5}, {y, -5, 5}, BoxRatios -> 1,
MeshStyle -> {({Red, Tube @@ #} &), Directive[Thick, Yellow], Directive[Thick, Cyan]},
MeshFunctions -> {#3 &, # &, #2 &}, Mesh -> {{-3}, 10, 5},