GRAPHVIZ: Force node to top of page

945 views Asked by At

I am using graphviz, however i woudl like to force the node "This on top" to the top of the page, and not side on. This is the graph:

enter image description here

This is the code:

g= Digraph('trial', filename='trial.gv')
g.attr(compound='true', rankdir="TB" )

with g.subgraph() as s:
  s.attr(rank='max')
  s.node('This on top ')
  s.edge('this right under', "Fabrication")

with g.subgraph(name='cluster0') as c:
    c.node("This")

    c.node("that")
    c.node("and this on the same level")
g.edge("this right under","that", lhead="cluster0" )
g.edge("that","This on top ", ltail="cluster0" )

g

Is there a command to make sure that Nodes are displayed in the TOP/Bottom order i wish?

1

There are 1 answers

0
Craig On BEST ANSWER

The first problem is that setting rank='max' is forcing everything in the first subgraph to go to the maximum rank, which is the lowest rank. You might have meant to set rank='min' which would place the items in the subgraph at the highest rank, but that still wouldn't create the arrangement you want.

Instead, you can use an invisible edge by setting style = 'invis' when creating the edge to force "This on top" to come before "this right under".

from graphviz import Digraph

g= Digraph('trial', filename='trial.gv')
g.attr(compound='true', rankdir="TB" )

with g.subgraph() as s:
  # s.attr(rank='min') # you don't need this line
  s.node('This on top ')
  s.edge('This on top ', 'this right under', style='invis') # add this invisible edge
  s.edge('this right under', "Fabrication")

with g.subgraph(name='cluster0') as c:
    c.node("This")

    c.node("that")
    c.node("and this on the same level")
g.edge("this right under", "that", lhead="cluster0" )
g.edge("that", "This on top ", ltail="cluster0", constraint="false" )

g

Which produces: Graph showing nodes stacked