Skip to content

Description:

  • PyGraphviz is a Python interface to the Graphviz graph layout and visualization package. With PyGraphviz you can create, edit, read, write, and draw graphs using Python to access the Graphviz graph data structure and layout algorithms.

Resources:

Create a simple example to create a graphviz dot file and draw a graph.

import pygraphviz as pgv
from pathlib import Path
from IPython.display import Image
tmp = "/tmp/demo_notebooks"
Path(tmp).mkdir(exist_ok=True)
A = pgv.AGraph()
A.add_edge(1, 2)
A.add_edge(2, 3)
A.add_edge(1, 3)
print(A.string()) # print to screen
A.write(f"{tmp}/simple.dot") # write to simple.dot
B = pgv.AGraph(f"{tmp}/simple.dot") # create a new graph from file
B.layout() # layout with default (neato)
B.draw(f"{tmp}/simple.png") # draw png (the format follows the file extension)
Image(filename=f"{tmp}/simple.png") # show the PNG in the notebook
Output
strict graph "" {
1 -- 2;
1 -- 3;
2 -- 3;
}
Figure drawn by code cell 1

ganymede is made by Eugo