2
$\begingroup$

How can I ask Mathematica to plot a rectangular grid (similar to the attached picture) with those red points on all crossings?

enter image description here

$\endgroup$
3
  • 3
    $\begingroup$ You tagged it graphs etc., so maybe GridGraph[{5, 4}, VertexStyle -> Red]? $\endgroup$
    – Michael E2
    Oct 30, 2021 at 0:45
  • $\begingroup$ @MichaelE2 Is not this network a graph? The code you mention gives a square network not rectangular. $\endgroup$
    – sara96
    Oct 30, 2021 at 0:56
  • 1
    $\begingroup$ All that can be adjusted. Show@GridGraph[{6, 7}, VertexStyle -> Red] /. GraphicsComplex[_, r__] :> GraphicsComplex[Tuples[N@{Range@7, 2 Range@6}], r] and GridGraph has an option VertexCoordinates, if you want a graph as indicated by the tag. $\endgroup$
    – Michael E2
    Oct 30, 2021 at 1:00

2 Answers 2

6
$\begingroup$
Clear[a, b, f, dx, dy];
{a, b} = {2, 3};
{dx, dy} = {Subdivide[0, a, 8], Subdivide[0, b, 5]};
f[x_, y_] = {x, y};
ParametricPlot[f @@ {x, y}, {x, 0, a}, {y, 0, b}, Mesh -> {dx, dy}, 
 Epilog -> {PointSize[Large], Red, Point[f @@@ Tuples[{dx, dy}]]}, 
 Frame -> False, Axes -> False, PlotStyle -> None, 
 MeshStyle -> Directive[AbsoluteThickness[3], Opacity[1], Black]]

enter image description here

We can test another function f[x_, y_] = {x^2 - y^2, x + y}; enter image description here

$\endgroup$
3
$\begingroup$

Make a table, modify range and increments:

t = Table[{x, y}, {x, 1, 4}, {y, 1, 4}]

Combine the following as needed:

g1 = Graphics@Line[#] & /@ t;
g2 = Graphics@Line[#] & /@ Transpose@t;
g3 = Graphics[{Red, PointSize[0.02], Point /@ t}];

Show[g1, g2, g3, Frame -> True, PlotRange -> {{0, 8}, {-1, 6}}, 
 AspectRatio -> Automatic]

enter image description here

Using Epilog or Prolog, the graphic (or parts of it) can be placed on a plot as required.

EDIT

Point grid can be changed in x and y directions; e.g., try:

t = Table[{x, y}, {x, Range[1, 5, 2]~Join~Range[9, 16, 3]}, {y, 1, 4}]

along with a slight change of plot range:

Show[g1, g2, g3, Frame -> True, PlotRange -> {{0, 18}, {-1, 6}}, 
 AspectRatio -> Automatic]

enter image description here

$\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.