Search code examples
pythongraphviz

How to use new node shapes in graphviz using python?


I am creating some graphviz graphs using python. The default shapes (Shown in the picture) are not enough for me. I want to use more shapes. How can I do this with python? enter image description here


Solution

  • One way is to use images containing whatever shape you may want to use:

    from graphviz import Digraph
    
    dot = Digraph(comment='The Round Table')
    
    #dot.node('A', label='King Arthur', shape='circle')
    dot.node('A', label='King Arthur', color='transparent', image='/path/to/star.png')
    dot.node('B', 'Sir Bedevere the Wise')
    dot.node('L', 'Sir Lancelot the Brave')
    
    dot.edges(['AB', 'AL'])
    dot.edge('B', 'L', constraint='false')
    
    dot.render('test-output/round-table.gv', view=True)
    

    This creates the following:

    enter image description here

    I believe there are some limitations to how this approach can be used, as noted in the documentation:

    If using SVG (-Tsvg), PostScript (-Tps,-Tps2) or one of the raster formats (-Tgif, -Tpng, or -Tjpg), you can load certain images (e.g., pictures) by file name into nodes.

    Another approach is to use HTML labels which allow you to use <IMG> attributes.