11
$\begingroup$

Is it possible to hide part of the mesh in a Plot3D plot? For example, in the plot

Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, Mesh -> {{1, 2, 3}, {1, 2, 3}}]

could I hide the mesh for x<2 and y<2?

My goal is to show a specific mesh in a Plot3D without modifying the internal mesh used for plotting, as this would be too expensive to get the desired mesh. The grid I want displayed is roughly structured, so I could plot it in the way above and just remove those parts of the lines I do not want.

$\endgroup$
4
  • 1
    $\begingroup$ Use a RegionFunction like this: Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, Mesh -> {{1, 2, 3}, {1, 2, 3}}, RegionFunction -> Function[{x, y, z}, ! (x < 2 && y < 2)]] or do you want to keep the surface too? $\endgroup$
    – flinty
    Commented Jan 10, 2022 at 17:52
  • $\begingroup$ Yes I want to keep the surface, I just want to control what information I show with the mesh lines. $\endgroup$ Commented Jan 10, 2022 at 18:06
  • 1
    $\begingroup$ Just a quick hack on @flinty's answer: Show[ Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, PlotRange -> {All, All, {-2, 8}}, Mesh -> {{1, 2, 3}, {1, 2, 3}}, RegionFunction -> Function[{x, y, z}, ! (x < 2 && y < 2)]], Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, Mesh -> None, RegionFunction -> Function[{x, y, z}, (x < 2 && y < 2)]] ] $\endgroup$ Commented Jan 10, 2022 at 18:29
  • 1
    $\begingroup$ @CraigCarter Better: Show[Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, PlotStyle -> None, Mesh -> {{1, 2, 3}, {1, 2, 3}}, PlotRange -> {All, All, {-2, 8}}, RegionFunction -> Function[{x, y, z}, ! (x < 2 && y < 2)]], Plot3D[x + y, {x, 0, 4}, {y, 0, 4}, Mesh -> None]]. $\endgroup$ Commented Jan 10, 2022 at 18:32

2 Answers 2

12
$\begingroup$

You may use the MeshFunctions option of Plot3D with ImplictRegion.

With region in OP

r = ImplicitRegion[x < 2 && y < 2, {x, y}];

then

Plot3D[x + y, {x, 0, 4}, {y, 0, 4}
 , Mesh -> {{1, 2, 3}, {1, 2, 3}}
 , MeshFunctions -> {
   If[{#1, #2} ∈ r, 0, #1] &
   , If[{#1, #2} ∈ r, 0, #2] &
   }
 ]

Mathematica graphics

You can use any region. However, for some you may have to increase the PlotPoints to get the mesh to connect nicely.

For example,

r2 = ImplicitRegion[(x - 2)^2 + (y - 2)^2 <= 1, {x, y}];

Plot3D[x + y, {x, 0, 4}, {y, 0, 4}
 , Mesh -> 5
 , MeshFunctions -> {
   If[{#1, #2} ∈ r2, 0, #1] &
   , If[{#1, #2} ∈ r2, 0, #2] &
   }
 , PlotPoints -> 100
 ]

Mathematica graphics

Easy to Manipluate as well.

Manipulate[
 region = 
  ImplicitRegion[(x - First@c)^2 + (y - Last@c)^2 <= 1, {x, y}];
 Plot3D[x + y, {x, 0, 4}, {y, 0, 4}
  , Mesh -> 5
  , MeshFunctions -> {
    If[{#1, #2} ∈ region, 0, #1] &
    , If[{#1, #2} ∈ region, 0, #2] &
    }
  , PlotPoints -> ControlActive[20, 100]
  ]
 , {{c, {2, 2}, "Center"}, {0, 0}, {4, 4}, {.01, .01}, 
  Appearance -> "Labeled"}
 , {region, None}
 ]

enter image description here

Hope this helps.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much, this does exactly what I want; I had a feeling this should be possible using MeshFunctions. It allows changing the mesh at no additional cost to the plot itself, except to evaluate the region. $\endgroup$ Commented Jan 11, 2022 at 11:12
7
$\begingroup$
Show[
 Plot3D[x + y, {x, 0, 2}, {y, 0, 2},
  Mesh -> None],
 Plot3D[ConditionalExpression[x + y, x >= 2 || y >= 2], 
  {x, 0, 4}, {y, 0, 4},
  Mesh -> {{1, 2, 3}, {1, 2, 3}}],
 PlotRange -> All]

enter image description here

$\endgroup$
4
  • $\begingroup$ With your solution, the surface itself is generated and rendered twice. To avoid this, add PlotStyle -> None: Show[Plot3D[x+y,{x,0,4},{y,0,4},Mesh->None],Plot3D[ConditionalExpression[x+y,x>=2||y>=2],{x,0,4},{y,0,4},Mesh->{{1,2,3},{1,2,3}},PlotStyle->None],PlotRange->All]. $\endgroup$ Commented Jan 10, 2022 at 19:00
  • $\begingroup$ @AlexeyPopkov - It is not generated twice. The first plot in only for 0 <= x <= 2 and 0 <= y <=2 $\endgroup$
    – Bob Hanlon
    Commented Jan 10, 2022 at 19:03
  • 1
    $\begingroup$ @AlexeyPopkov - for a moderately "expensive" function (e.g., f[x_, y_] := Log[PDF[BinormalDistribution[{2, 2}, {1, 1}, 0.5], {x, y}]], RepeatedTiming indicates that my suggested approach is more efficient. $\endgroup$
    – Bob Hanlon
    Commented Jan 10, 2022 at 19:18
  • $\begingroup$ Yes, but RegionFunction approach still wins both in the speed and LeafCount of the obtained expression. $\endgroup$ Commented Jan 10, 2022 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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