1
$\begingroup$

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]]]]]  

enter image description here

  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?

enter image description here

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

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – glS Mar 25 '17 at 23:36
  • $\begingroup$ 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). $\endgroup$ – Steve Mar 25 '17 at 23:40
  • $\begingroup$ 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. $\endgroup$ – Steve Mar 25 '17 at 23:43
4
$\begingroup$
Plot3D[-Sqrt[x^2 + y^2], {x, -5, 5}, {y, -5, 5}, BoxRatios -> 1, 
 MeshStyle -> Directive[Thick, Red],  
 MeshFunctions -> {#3 &}, Mesh -> {{-3}}, 
 MeshShading -> {Blue, Opacity[.4, White]}]

Mathematica graphics

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] &) 

Mathematica graphics

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}, 
 MeshShading -> {{{Blue, Opacity[0.4, White]}}}]

Mathematica graphics

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes, very good. Thank you! i.imgur.com/E362CgA.png $\endgroup$ – Steve Mar 25 '17 at 23:56
  • $\begingroup$ Is there a good way to get the x,y meshlines back without disrupting this solution? i.imgur.com/jV9F2NH.png $\endgroup$ – Steve Mar 25 '17 at 23:59
  • $\begingroup$ I just can't figure out what rank it needs: i.imgur.com/dx7AxjZ.png $\endgroup$ – Steve Mar 26 '17 at 0:08
  • $\begingroup$ @Steve, thank you for the accept, Re adding the x and y mesh lines please see the update. $\endgroup$ – kglr Mar 26 '17 at 0:14
  • $\begingroup$ Thanks so much, your solution is perfect for me. However, for anyone who might read this page in the future, the Tube @@ # solution for the line seems to tie the size to the axes scale. For example, in my plot with x,y axes on the order of 10^(-12) this red "tube" fills the entire plot red. It works fine with just a Directive[Red,Thickness[0.01]] type MeshStyle though. Cheers. $\endgroup$ – Steve Mar 26 '17 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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