# How to draw horizontal and vertical edges between to node?

**URL:** <https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299>\
**Category:** Help\
**Created:** [August 31, 2022, 10:21am UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299 "2022-08-31T10:21:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nihar](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nihar](https://forum.graphviz.org/u/Nihar)\
**Post date:** [August 31, 2022, 10:21am UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299/1 "2022-08-31T10:21:20Z")

</div>

can we place horizontal and vertical direction with edge length in a directed graph using grapviz ?  
 ![image_2022_08_31T10_20_18_448Z](https://global.discourse-cdn.com/graphviz/original/1X/bde2bb8a96f0cc371983a78cd2eb018a93300618.png)

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [September 1, 2022, 6:25pm UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299/2 "2022-09-01T18:25:14Z")

</div>

- You can create images like the above, if you are willing/able to position/locate the nodes yourself
- However, I don’t believe that any of the Graphviz engines will produce a graph anywhere close to the above “on their own” (without lots of user guidance)

Here is a Graphviz file that can be fed to **neato** (not **dot** ) to produce a copy of your graph. Note the use of the **pin** and **pos** attributes

```auto
 digraph HV {
  splines=false
  node [label="" width=.2 height=.15 pin=true]
   "n3,4" [pos="4,3"]
   "n5,7" [pos="7,5"]
   "n3,5" [pos="5,3"]
   "n1,2" [pos="2,1"]
   "n3,6" [pos="6,3"]
   "n4,1" [pos="1,4"]
   "n6,5" [pos="5,6"]
   "n3,7" [pos="7,3"]
   "n4,2" [pos="2,4"]
   "n3,8" [pos="8,3"]
   "n1,5" [pos="5,1"]
   "n4,3" [pos="3,4"]
   "n4,4" [pos="4,4"]
   "n4,5" [pos="5,4"]
   "n2,2" [pos="2,2"]
   "n2,3" [pos="3,2"]
   "n4,6" [pos="6,4"]
   "n2,4" [pos="4,2"]
   "n4,7" [pos="7,4"]
   "n2,5" [pos="5,2"]
   "n5,3" [pos="3,5"]
   "n4,8" [pos="8,4"]
   "n2,6" [pos="6,2"]
   "n4,9" [pos="9,4"]
   "n2,7" [pos="7,2"]
   "n5,5" [pos="5,5"]
   "n3,2" [pos="2,3"]
   "n2,8" [pos="8,2"]
   "n3,3" [pos="3,3"]
   "n5,6" [pos="6,5"]
   "n2,2" -> "n1,2"
   "n3,2" -> "n2,2"
   "n4,2" -> "n3,2"
   "n4,1" -> "n4,2"
   "n4,2" -> "n4,3"
   "n4,3" -> "n4,4"
   "n4,4" -> "n4,5"
   "n4,5" -> "n4,6"
   "n4,6" -> "n4,7"
   "n4,7" -> "n4,8"
   "n4,8" -> "n4,9"
   "n5,5" -> "n5,6"
   "n4,5" -> "n5,5"
   "n4,3" -> "n5,3"
   "n4,7" -> "n5,7"
   "n2,2" -> "n2,3"
   "n3,2" -> "n3,3"
   "n3,3" -> "n3,4"
   "n2,5" -> "n2,4"
   "n2,5" -> "n1,5"
   "n2,5" -> "n2,6"
   "n3,5" -> "n3,6"
   "n3,7" -> "n2,7"
   "n4,7" -> "n3,7"
   "n4,8" -> "n3,8"
   "n2,7" -> "n2,8"
   "n5,5" -> "n6,5"
   "n4,5" -> "n3,5"
   "n3,5" -> "n2,5"
}

```

Giving:

 ![o](https://global.discourse-cdn.com/graphviz/original/1X/34ea53ddc68850626f33b82252ecc94160e5c263.png)

---

<div class="post-metadata">

**Author:** ![Nihar](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Nihar](https://forum.graphviz.org/u/Nihar)\
**Post date:** [September 2, 2022, 10:56am UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299/3 "2022-09-02T10:56:47Z")

</div>

ok thanks for your comment

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [September 2, 2022, 7:55pm UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299/4 "2022-09-02T19:55:41Z")

</div>

Just because I was curious, I applied some of the Graphviz engines to the above input file with `pin=false`.

**dot** - oh look, it is a tree. (I had not recognized that)

 ![oo.dot](https://global.discourse-cdn.com/graphviz/original/1X/5b901566933e5b56485006b7b34c5269faebc909.png)

**fdp** - surprisingly close to the desired horizontal/vertical layout, but a challenge to programmatically do the transform

![oo.fdp](https://global.discourse-cdn.com/graphviz/original/1X/a4b1c7ebfbfd7b258f293902740c345ae5f4404a.png)

**neato** (without pinning) - very similar to **fdp**

 ![oo.neato](https://global.discourse-cdn.com/graphviz/original/1X/888e02ec341557782936b96dee324ff77a295d8b.png)

So, could one come up with an algorithm to produce the desired graph (or equivalent) without having positions provided by the user?

---

<div class="post-metadata">

**Author:** ![pof](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/pof/32/800_2.png) [@pof](https://forum.graphviz.org/u/pof)\
**Post date:** [September 19, 2022, 3:06pm UTC](https://forum.graphviz.org/t/how-to-draw-horizontal-and-vertical-edges-between-to-node/1299/5 "2022-09-19T15:06:46Z")

</div>

You can create [grid](https://graphviz.org/Gallery/undirected/grid.html) with some visible and some invisible edges.

![img](https://global.discourse-cdn.com/graphviz/original/1X/44c3ba9d666974d87c46da3feb164523e8efea37.svg)

```auto
graph grid
{
	fontname="Helvetica,Arial,sans-serif"
	node [fontname="Helvetica,Arial,sans-serif"]
	edge [fontname="Helvetica,Arial,sans-serif"]
	layout=dot
	label="grid"
	labelloc = "t"
	node [shape=plaintext]
	// arbitrary path on rigid grid
	A0 -- B1 -- C2 -- D3 -- E4 -- F5 -- G6 -- H7
	H0 -- G1 -- F2 -- E3 -- D4 -- C5 -- B6 -- A7

	edge [weight=1000 style=dashed color=dimgrey]

	// uncomment to hide the grid
	//edge [style=invis]

	A0 -- A1 -- A2 -- A3 -- A4 -- A5 -- A6 -- A7
	B0 -- B1 -- B2 -- B3 -- B4 -- B5 -- B6 -- B7
	C0 -- C1 -- C2 -- C3 -- C4 -- C5 -- C6 -- C7
	D0 -- D1 -- D2 -- D3 -- D4 -- D5 -- D6 -- D7
	E0 -- E1 -- E2 -- E3 -- E4 -- E5 -- E6 -- E7
	F0 -- F1 -- F2 -- F3 -- F4 -- F5 -- F6 -- F7
	G0 -- G1 -- G2 -- G3 -- G4 -- G5 -- G6 -- G7
	H0 -- H1 -- H2 -- H3 -- H4 -- H5 -- H6 -- H7

	rank=same {A0 -- B0 -- C0 -- D0 -- E0 -- F0 -- G0 -- H0}
	rank=same {A1 -- B1 -- C1 -- D1 -- E1 -- F1 -- G1 -- H1}
	rank=same {A2 -- B2 -- C2 -- D2 -- E2 -- F2 -- G2 -- H2}
	rank=same {A3 -- B3 -- C3 -- D3 -- E3 -- F3 -- G3 -- H3}
	rank=same {A4 -- B4 -- C4 -- D4 -- E4 -- F4 -- G4 -- H4}
	rank=same {A5 -- B5 -- C5 -- D5 -- E5 -- F5 -- G5 -- H5}
	rank=same {A6 -- B6 -- C6 -- D6 -- E6 -- F6 -- G6 -- H6}
	rank=same {A7 -- B7 -- C7 -- D7 -- E7 -- F7 -- G7 -- H7}
}
// grid.dot by Costa Shulyupin

```
