# Some nodes on wrong layer

**URL:** <https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390>\
**Category:** Help\
**Created:** [November 23, 2022, 6:25pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390 "2022-11-23T18:25:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andey](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/andey/32/884_2.png) [@andey](https://forum.graphviz.org/u/andey)\
**Post date:** [November 23, 2022, 6:25pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/1 "2022-11-23T18:25:46Z")

</div>

Hello,

I am creating some graphs (random edges on each run) with the help of a python library, however I get some graphs with a layout I do not want.

I need a three layered graph, the green triangles in the top row, the orange ellipses in the middle and the blue rectangles on the bottom row.  
However, sometimes there are some orange ones in the bottom row, and I can not understand why this is happening or how I can prevent this.

Thanks and kind regards  
Andreas

How some graphs look like:

 ![grafik](https://global.discourse-cdn.com/graphviz/original/1X/d13350632961b3ea6feeb1841744c2619f7d19da.png)

How they should look like:

 ![grafik](https://global.discourse-cdn.com/graphviz/original/1X/d3386c18bbaf9893c52e29bc19d5466c5f107263.png)

Working example:  
[https://dreampuf.github.io/GraphvizOnline/#%2F%2F%20Graph%20Layout digraph%20{ node%20[height%3D.1] rankdir%3DTB node%20[shape%3Dtriangle] { rank%3Dsame a%20[fillcolor%3D"%23c7f867"%20style%3Dfilled] b%20[fillcolor%3D"%23c7f867"%20style%3Dfilled] } node%20[shape%3Dellipse] { 0%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 1%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 2%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 3%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 4%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 5%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 6%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 7%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 8%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] 9%20[fillcolor%3D"%23ff8945"%20style%3Dfilled] } node%20[shape%3Dbox] { rank%3Dsame x1%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] x2%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] y1%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] y2%20[fillcolor%3D"%2360f3ff"%20style%3Dfilled] } a%20-\>%200 b%20-\>%200 a%20-\>%201 b%20-\>%201 1%20-\>%202 1%20-\>%202 1%20-\>%203 0%20-\>%203 0%20-\>%204 3%20-\>%204 b%20-\>%205 a%20-\>%205 4%20-\>%206 a%20-\>%206 4%20-\>%207 0%20-\>%207 b%20-\>%208 5%20-\>%208 3%20-\>%209 8%20-\>%209 2%20-\>%20x1 5%20-\>%20x2 2%20-\>%20y1 4%20-\>%20y2 }](https://dreampuf.github.io/GraphvizOnline/#%2F%2F%20Graph%20Layout%0Adigraph%20%7B%0A%09node%20%5Bheight%3D.1%5D%0A%09rankdir%3DTB%0A%09node%20%5Bshape%3Dtriangle%5D%0A%09%7B%0A%09%09rank%3Dsame%0A%09%09a%20%5Bfillcolor%3D%22%23c7f867%22%20style%3Dfilled%5D%0A%09%09b%20%5Bfillcolor%3D%22%23c7f867%22%20style%3Dfilled%5D%0A%09%7D%0A%09node%20%5Bshape%3Dellipse%5D%0A%09%7B%0A%09%090%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%091%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%092%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%093%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%094%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%095%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%096%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%097%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%098%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%099%20%5Bfillcolor%3D%22%23ff8945%22%20style%3Dfilled%5D%0A%09%7D%0A%09node%20%5Bshape%3Dbox%5D%0A%09%7B%0A%09%09rank%3Dsame%0A%09%09x1%20%5Bfillcolor%3D%22%2360f3ff%22%20style%3Dfilled%5D%0A%09%09x2%20%5Bfillcolor%3D%22%2360f3ff%22%20style%3Dfilled%5D%0A%09%09y1%20%5Bfillcolor%3D%22%2360f3ff%22%20style%3Dfilled%5D%0A%09%09y2%20%5Bfillcolor%3D%22%2360f3ff%22%20style%3Dfilled%5D%0A%09%7D%0A%09a%20-%3E%200%0A%09b%20-%3E%200%0A%09a%20-%3E%201%0A%09b%20-%3E%201%0A%091%20-%3E%202%0A%091%20-%3E%202%0A%091%20-%3E%203%0A%090%20-%3E%203%0A%090%20-%3E%204%0A%093%20-%3E%204%0A%09b%20-%3E%205%0A%09a%20-%3E%205%0A%094%20-%3E%206%0A%09a%20-%3E%206%0A%094%20-%3E%207%0A%090%20-%3E%207%0A%09b%20-%3E%208%0A%095%20-%3E%208%0A%093%20-%3E%209%0A%098%20-%3E%209%0A%092%20-%3E%20x1%0A%095%20-%3E%20x2%0A%092%20-%3E%20y1%0A%094%20-%3E%20y2%0A%7D%0A)

---

<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:** [November 23, 2022, 9:03pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/2 "2022-11-23T21:03:16Z")

</div>

Your Graphviz program is unreadable. Please consult this “PSA” (public service announcement) [Center a node above a group of other nodes - #4 by smattr](https://forum.graphviz.org/t/center-a-node-above-a-group-of-other-nodes/1165/4) and “fix” the readability of the program. Thanks

---

<div class="post-metadata">

**Author:** ![andey](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/andey/32/884_2.png) [@andey](https://forum.graphviz.org/u/andey)\
**Post date:** [November 23, 2022, 9:25pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/3 "2022-11-23T21:25:36Z")

</div>

Thanks for your answer, it’s a clickable link to graphviz online. I am not sure if I can shorten links in a post?

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [November 23, 2022, 9:35pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/4 "2022-11-23T21:35:01Z")

</div>

There’s some option to put nodes on the highest possible level (at least if there is slack in the assignment) if someone can find it or if it was ever documented 🙂

---

<div class="post-metadata">

**Author:** ![jjlong](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/jjlong/32/887_2.png) [@jjlong](https://forum.graphviz.org/u/jjlong)\
**Post date:** [November 23, 2022, 10:01pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/5 "2022-11-23T22:01:26Z")

</div>

In your third subgraph with the box nodes, change the statement:

rank=same

To:

rank=sink

and you will get the effect you seek.

 ![image](https://global.discourse-cdn.com/graphviz/original/1X/fad6541e151851ae79a790685121ed73f2e28393.jpeg)

---

<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:** [November 23, 2022, 10:16pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/6 "2022-11-23T22:16:55Z")

</div>

[fyi, **layers** means something else to Graphviz ([FAQ | Graphviz](http://www.graphviz.org/faq/#FaqOverlays)), but we’ll ignore that]

**Dot** tries to minimize the number of ranks in a graph. While **rank=same** forces a set of nodes to appear on the same rank, it does not prevent **dot** from placing other nodes on that same rank. You created a set of nodes, but not an exclusive set of nodes.  
Since you are setting the top and bottom “layers” to be single ranks, all you need to do is set the top layer to **rank=source** and the bottom layer to **rank=sink** ([rank | Graphviz](http://www.graphviz.org/docs/attrs/rank/)) This will make the sets exclusive.  
Like so:

```auto
digraph {
	node [height=.1]
	rankdir=TB
	node [shape=triangle]
	{
		rank=source // changed
		a [fillcolor="#c7f867" style=filled]
		b [fillcolor="#c7f867" style=filled]
	}
	node [shape=ellipse]
	{
		0 [fillcolor="#ff8945" style=filled]
		1 [fillcolor="#ff8945" style=filled]
		2 [fillcolor="#ff8945" style=filled]
		3 [fillcolor="#ff8945" style=filled]
		4 [fillcolor="#ff8945" style=filled]
		5 [fillcolor="#ff8945" style=filled]
		6 [fillcolor="#ff8945" style=filled]
		7 [fillcolor="#ff8945" style=filled]
		8 [fillcolor="#ff8945" style=filled]
		9 [fillcolor="#ff8945" style=filled]
	}
	node [shape=box]
	{
		rank=sink // changed
		x1 [fillcolor="#60f3ff" style=filled]
		x2 [fillcolor="#60f3ff" style=filled]
		y1 [fillcolor="#60f3ff" style=filled]
		y2 [fillcolor="#60f3ff" style=filled]
	}
	a -> 0
	b -> 0
	a -> 1
	b -> 1
	1 -> 2
	1 -> 2
	1 -> 3
	0 -> 3
	0 -> 4
	3 -> 4
	b -> 5
	a -> 5
	4 -> 6
	a -> 6
	4 -> 7
	0 -> 7
	b -> 8
	5 -> 8
	3 -> 9
	8 -> 9
	2 -> x1
	5 -> x2
	2 -> y1
	4 -> y2
}

```

Giving:

 ![betterRanks1](https://global.discourse-cdn.com/graphviz/original/1X/ab89220908efa60ba7508c67199b79bf72f83e9e.png)

---

<div class="post-metadata">

**Author:** ![andey](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/andey/32/884_2.png) [@andey](https://forum.graphviz.org/u/andey)\
**Post date:** [November 26, 2022, 6:06pm UTC](https://forum.graphviz.org/t/some-nodes-on-wrong-layer/1390/7 "2022-11-26T18:06:41Z")

</div>

Thanks, using “sink” and “source” solved my issue. 🙂

A note in the docs which says that those make the nodes exclusive would be nice for beginners like me 🙂
