Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

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

3 Answers 3

up vote 5 down vote accepted
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

share|improve this answer
    
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? –  Algohi Jul 3 at 18:30
    
What do you mean by " for grid with not equal spaces"? –  belisarius Jul 3 at 18:45
    
@Algohi You can modify the VertexCoordinates so.., yes? –  Öskå Jul 3 at 18:46
    
@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. –  Algohi Jul 3 at 18:51
    
@Algohi Easy. Just adjust the vc calculation –  belisarius Jul 3 at 19:08
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

share|improve this answer

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];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.