# Hide mesh in some parts of Plot3D

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.

• 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? Commented Jan 10, 2022 at 17:52
• Yes I want to keep the surface, I just want to control what information I show with the mesh lines. Commented Jan 10, 2022 at 18:06
• 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)]] ] Commented Jan 10, 2022 at 18:29
• @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]]. Commented Jan 10, 2022 at 18:32

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


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
]


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


Hope this helps.

• 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. Commented Jan 11, 2022 at 11:12
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]


• 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]. Commented Jan 10, 2022 at 19:00
• @AlexeyPopkov - It is not generated twice. The first plot in only for 0 <= x <= 2 and 0 <= y <=2 Commented Jan 10, 2022 at 19:03
• @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. Commented Jan 10, 2022 at 19:18
• Yes, but RegionFunction approach still wins both in the speed and LeafCount of the obtained expression. Commented Jan 10, 2022 at 19:28