News

New paper! in the American Naturalist

Wednesday, May 23, 2018

Visualise a graph with graphviz



Draw a graph with graphviz. This works for directed multigraphs with self loops. Easy to show weights as labels.
from graphviz import Digraph

def drawGraph(mat):
 g = Digraph('G', filename='graph.gv')
 # G: name allocated to this graph
 # filename: used for output file
 # mat: weighted adjacency matrix
 n = len(mat)
 for i in range(n):
  for j in range(n):
   if mat[i,j] > 0.:
    g.edge(str(j),str(i), label=str(mat[i,j]))
    # arrow goes j -> i
    # label: edge label to show
 return g

g = drawGraph(mat)
g.view() # show the graph and save as filename.pdf