How can I ensure that nodes are rendered in the order that they are defined in my source code? In my code, I have node 'A' defined first, and so was hoping it would be rendered at the 'top' of the graph, and then the three 'child class' nodes would appear below it (all in a row as they are in the embedded image). but as can be see in the embedded image, the opposite has occurred. I am using Python 3.7.12 in a Jupyter Notebook.
from IPython.display import display
import graphviz
p = graphviz.Digraph(node_attr={'shape':'box'},format='png')
p.node(name='A', label='parent class')
with p.subgraph() as s:
s.attr(rank='same')
s.node(name='B', label='child class 1')
s.node(name='C', label='child class 2')
s.node(name='D', label='child class 3')
p.edges(['BA', 'CA', 'DA'])
p.render('a.gv',view=False)
with open('a.gv') as f:
dot = f.read()
display(graphviz.Source(dot))
Does anyone know how to fix this?
OK I just had to set the 'rankdir' graph attribute to 'BT' which I believe means 'bottom-top'
here's the updated code:
from IPython.display import display
import graphviz
p = graphviz.Digraph(graph_attr={'rankdir':"BT"},
node_attr={'shape':'box'},
format='png')
p.node(name='A', label='parent class')
with p.subgraph() as s:
s.attr(rank='same')
s.node(name='B', label='child class 1')
s.node(name='C', label='child class 2')
s.node(name='D', label = 'child class 3')
p.edges(['BA', 'CA', 'DA'])
p.render('a.gv',view=False)
with open('a.gv') as f:
dot = f.read()
display(graphviz.Source(dot))