# Lattice lines with integer coordinates in 3D plot

I would like to draw a surface in a 3D plot together with gridlines (horizontal lines and vertical lines) parallel to x y z axes with integer coordinates. Of course not only on faces, but also inside the box. For example

Plot3D[{Sqrt[16- x^2 - y^2]}, {x, -4, 4}, {y, -4, 4},
PlotStyle -> Blue, AxesLabel -> Automatic]


with grid lines pass through all integers in plot region.

• And what have you tried? Mathematica is really hard to learn only by reading other people's code – Dr. belisarius Sep 9 '15 at 18:28

This is slightly more/different than OP requested, but why not?

g = Plot3D[Sqrt[16 - x^2 - y^2], {x, -4, 4}, {y, -4, 4},
Mesh -> None, PlotStyle -> Opacity[.7]] Next define a function that gets us all the rectangles we need, wraps them into a Head of Polygon and overlays them onto the original plot:

addgrids[g_Graphics3D] :=
Module[{ranges = {Floor@#1, Ceiling@#2} & @@@ PlotRange@g, rects},
rects =
Function[{perm},
Map[Function[{points}, Prepend[points, #]],
Tuples[Rest@#][[{1, 2, 4, 3}]] &@perm] & /@
Range @@ First@perm] /@
(RotateRight[ranges, #] & /@ Range);
rects =
MapIndexed[Function[{list, amount},
Map[RotateLeft[#, First@amount] &, list, {2}]], rects];
Show[{g,
Graphics3D[{Opacity[.2], Polygon@Flatten[rects, 1]}]
}]
]


Then simply post-process the plot with this function:

g // addgrids While I'm at it, here's a similar approach for adding grid-lines, rather than grid-surfaces:

addlines[g_Graphics3D] :=
Module[{ranges = {Floor@#1, Ceiling@#2} & @@@ PlotRange@g, lines},
lines =
Function[{perm},
Flatten[
Block[{i, j, Table = Table}, ClearAttributes[Table, HoldAll];
(Table[{{First@First@perm, i, j}, {Last@First@perm, i, j}},
{i, Sequence @@ #1}, {j, Sequence @@ #2}] & @@ (Rest@
perm))], 1]] /@
(RotateRight[ranges, #] & /@ Range);
lines =
MapIndexed[Function[{list, amount},
Map[RotateLeft[#, First@amount] &, list, {2}]], lines];
Show[{g,
Graphics3D[Line@Flatten[lines, 1]]
}]
] Just something to get you started:

rng = {{0, 5.8}, {0, 6.9}};
pts = Tuples[Range @@@ IntegerPart@rng];
f[x_, y_] := x^2 + y^2

Show[
Plot3D[f[x, y], Evaluate[Join[{x}, First@rng]], Evaluate[Join[{y}, Last@rng]]],
Graphics3D[(Line@{Append[#, 0], Append[#, f @@ ##]} & /@ pts)]] Maybe something as simple as the FaceGrids option will work for you.

Plot3D[Exp[-(x^2 + y^2)], {x, -2, 2}, {y, -2, 2},
PlotTheme -> "ZMesh",
PlotRange -> All,
FaceGrids -> All] • I edited my question. maybe it is now more clear. Thanks. – asad Sep 10 '15 at 4:30

You could Use the Show function to join a Graphics object containing your lines to you Graph:

lines = {{{0, -2, 0}, {0, 2, 0}}, {{-2, 1, 0}, {2, 1, 0}}, {{-2,
1, -.5}, {2, 1, -.5}}, {{-2, 1, .5}, {2, 1, .5}}, {{1, -2,
0}, {1, 2, 0}}};

Show[Plot3D[{Sin[x + y^2]/2}, {x, -3, 3}, {y, -2, 2},
PlotStyle -> Opacity[0.2]], Graphics3D[{Red, Line[lines]}]] 