2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ And what have you tried? Mathematica is really hard to learn only by reading other people's code $\endgroup$ Sep 9, 2015 at 18:28

4 Answers 4

4
$\begingroup$

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

Sphere

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[3]);
  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

with grids

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[3]);
  lines =
   MapIndexed[Function[{list, amount},
     Map[RotateLeft[#, First@amount] &, list, {2}]], lines];
  Show[{g,
    Graphics3D[Line@Flatten[lines, 1]]
    }]
  ]

g // addlines

gridlines

$\endgroup$
5
$\begingroup$

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

Mathematica graphics

$\endgroup$
3
$\begingroup$

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]

plot3d

$\endgroup$
1
  • $\begingroup$ I edited my question. maybe it is now more clear. Thanks. $\endgroup$
    – asad
    Sep 10, 2015 at 4:30
1
$\begingroup$

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

Lines in a simple Sine plot

$\endgroup$

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.