Using dot for graphs

asked 2014-02-01 17:33:54 +0100

anonymous user

Anonymous

updated 2015-01-17 22:28:06 +0100

FrédéricC gravatar image

The graphs should be rendered by 'dot', but layout='graphviz' fails due to an error in the distribution's dot2tex package. So I came up with the following:

#auto
def dot(g, otype='png'):
    dottmpfile = 'g'+str(g.graphviz_string().__hash__())+otype
    g.graphviz_to_file_named(dottmpfile, edge_labels=True)
    import subprocess
    subprocess.call(['dot', '-T'+otype, '-o', dottmpfile+'.'+otype, dottmpfile])
    os.remove(dottmpfile)

Which allows to generate graphs like this:

g = DiGraph({'a' : ['a', 'b'], 'b' : ['b','c'], 'c' : ['d'], 'd' : ['c']}, loops=True)
dot(g)

Is there a better way of calling dot straight?

edit retag flag offensive close merge delete