Search code examples
pythonjupyter-notebookgraphviz

How to create straight edges at specific corners using graphviz


I have the below code to create a simple flow using graphviz to be visualized in jupyter notebook

# Create Digraph object
dot = Digraph()

# Add nodes 
dot.node('1', shape='box')
dot.node('2', shape='rectangle')
dot.node('3', shape='parallelogram')
dot.node('4', shape='diamond')
dot.node('5', shape='box')

# Add edge between nodes
dot.edges(['12', '23', '34', '42', '45' ])

# Visualize the graph
dot

How to make an edge from decision box right corner to box 2 a straight line?


Solution

  • Pls define edges and tailport='e', headport='e' helps to define the right side (e=east). tailport: from where, headport: to where.

    Code:

    from graphviz import Digraph
    
    dot = Digraph(graph_attr={'splines': 'ortho'})
    
    dot.node('1', shape='box')
    dot.node('2', shape='rectangle')
    dot.node('3', shape='parallelogram')
    dot.node('4', shape='diamond')
    dot.node('5', shape='box')
    
    dot.edge('1', '2')
    dot.edge('2', '3')
    dot.edge('3', '4')
    dot.edge('4', '2', tailport='e', headport='e', constraint='false')
    dot.edge('4', '5')
    
    dot
    

    Output:

    enter image description here