3
$\begingroup$

I have the following simple code. I need to connect each point in the plot with all adjacent points in vertical or horizontal line.

n = 5;
nodes = Flatten[
   Table[{i (16 2.54)/n, j (16 2.54)/n}, {i, 0, n}, {j, 0, n}], 1];
dataPlot = ListPlot[nodes, PlotStyle -> PointSize -> Large];
nodelabels = 
  Table[Text[
    Style[i + (n + 1) j + 1, 14, Bold], {0.7 + i (16 2.54)/n, 
     0.7 + j (16 2.54)/n}], {i, 0, n}, {j, 0, n}];
elementlabels = 
  Table[Text[
    Style[i + (n) j + 1, 14, 
     Bold], {(16 2.54)/(2 n) + i (16 2.54)/n, (16 2.54)/(2 n) + 
      j (16 2.54)/n}], {i, 0, n - 1}, {j, 0, n - 1}];
Show[Graphics[{{Red, nodelabels}, {elementlabels}}], dataPlot, 
 AspectRatio -> 1, Axes -> True, ImageSize -> 600]

I have manually added some lines to show what I am looking for. enter image description here

note: there is a way to do that using ListLinePlot but this method requires to overlay so many ListLinePlot. I would prefer to get solution using Vertex functions.

$\endgroup$
6
  • $\begingroup$ I don't get it. What should be the result in the end? $\endgroup$
    – Öskå
    Jul 3, 2014 at 17:53
  • 1
    $\begingroup$ This? dataPlot=ListPlot[nodes,PlotStyle->PointSize->Large,GridLines->Union/@Transpose@nodes] $\endgroup$
    – mfvonh
    Jul 3, 2014 at 17:54
  • $\begingroup$ Or just GridGraph? $\endgroup$
    – Öskå
    Jul 3, 2014 at 17:58
  • $\begingroup$ @mfvonh, thanks for the answer. the grid did not appear with show? can you look at this issue? $\endgroup$ Jul 3, 2014 at 18:03
  • $\begingroup$ @mfvonh. ok I got it. the GridLines has to be placed in the Show function. Thanks $\endgroup$ Jul 3, 2014 at 18:08

3 Answers 3

5
$\begingroup$
n = 5;
g = GridGraph[{n + 1, n + 1}];
vc = SortBy[ PropertyValue[{g, #}, VertexCoordinates] & /@ 
                                   VertexList@g, Last] # - # &@(16 2.54/n);
g1 = SetProperty[g, {VertexCoordinates -> vc, VertexLabels -> "Name", 
                    ImagePadding -> 10, VertexStyle -> Red, VertexSize -> Small}];
Show[g1, PlotRange -> {{-1, 9 n}, {-1, 9 n}}, AspectRatio -> 1, Axes -> True]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ Great. you may change the plot rang to PlotRange -> {{-1, 16 2.54 + 1}, {-1, 16 2.54 + 1}} for cases when n is larger than 5. can this answer work for grid with not equal spaces? $\endgroup$ Jul 3, 2014 at 18:30
  • $\begingroup$ What do you mean by " for grid with not equal spaces"? $\endgroup$ Jul 3, 2014 at 18:45
  • $\begingroup$ @Algohi You can modify the VertexCoordinates so.., yes? $\endgroup$
    – Öskå
    Jul 3, 2014 at 18:46
  • $\begingroup$ @Belisarius I mean not square grids. if the distance between nodes 1 and 2 vertical grids is not the same as that between nodes 2 and 3 or 3 and 4. some thing like this. $\endgroup$ Jul 3, 2014 at 18:51
  • $\begingroup$ @Algohi Easy. Just adjust the vc calculation $\endgroup$ Jul 3, 2014 at 19:08
3
$\begingroup$
ClearAll[ggF];
ggF[n_, m_, sc1_, sc2_, opts : OptionsPattern[Graph]] := 
       GridGraph[{n + 1, m + 1}, VertexCoordinates -> (Join @@ 
                 Array[{sc1, sc2} {#2, #1} &, {m + 1, n + 1}, 0]), opts]; 
      (* ignore the red syntax highligting *)

options = {VertexLabels -> "Name", VertexStyle -> Red, 
         VertexSize -> Small, Axes -> True, ImagePadding -> 20, AxesOrigin -> {0, 0}};

ggF[5, 5, 16 2.54/5, 16 2.54/5, ImageSize -> 400, options]

enter image description here

ggF[5, 3, 16 2.54/5, 16 2.54/3, ImageSize -> 400, options]

enter image description here

ggF[5, 3, 16 2.54/5, 16 2.54/5, ImageSize -> 400, options]

enter image description here

$\endgroup$
3
$\begingroup$

In case you want to preserve the Graphics form, note that the Table used to generate nodes returns the vertical lines of those you seek. Transpose to get the horizontal lines.

For example:

vertlines  = Table[{i (16 2.54)/n, j (16 2.54)/n}, {i, 0, n}, {j, 0, n}];
lines      = Flatten[{vertlines, Transpose@vertlines}, 1];
nodes      = Flatten[vertlines, 1];
(* other defs. as is *)

Show[Graphics[{Line[lines], {Red, nodelabels}, {elementlabels}}], dataPlot,
 AspectRatio -> 1, Axes -> True]

Mathematica graphics

Or if you're concerned about efficiency and wish to omit the interior points of the lines, then use

lines = Flatten[{vertlines, Transpose@vertlines}[[All, All, {1, -1}]], 1];
$\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.