pythonchartsgraphviz

Is it possible to set the nodes and edges positions specified by user in Python Graphviz?


I'd like to build the following chart (I did it NOT in Graphviz): enter image description here

I tried to do it in Python Graphviz with the following code:

g = Digraph(engine='neato')
g.node('1', label='Very long name', pos='0,0')
g.node('2',label='Another very long name', pos='0,-1')
g.edge('1', '2')

g.node('3','Very long name again', pos='0,-2')
g.edge('2', '3')
g.node('4', 'Another very long name', pos='10,-3')
g.edge('3', '4')

g

which outputs the following: enter image description here

For controlling the position of nodes I tried to use neato engine, but it seems to me, that it does not work properly, so I have no idea how to control the position of nodes and edges in Graphviz, is it possible in principle?


Solution

  • You can use the rank attribute to control this in a subgraph of g.

    from graphviz import Digraph
    
    g = Digraph()
    
    g.node('1', 'Very long name')
    g.node('2', 'Another very long name')
    g.node('3', 'Very long name again')
    g.node('4', 'Another very long name')
    
    g.edges(['12', '23'])
    
    with g.subgraph() as s:
      s.attr(rank='same')
      s.edge('3', '4')
    
    g