PyGraphviz example

Example of visualising graphs with networkx and using the PyGraphviz backend for visualising graphs. This produces nicer graph images than the Matplotlib backend, but PyGraphviz is problematic to with conda.


In [4]:
import networkx as nx

# Create a directed networkx graph
G = nx.DiGraph()

# Add web pages (graph nodes)
for i in range(5):
   G.add_node(i, label="p" + str(i))

# Add outgoing web links with weights (directed graph edges)
G.add_edge(0, 1, weight=1.0/4.0, label="1/4")
G.add_edge(0, 2, weight=1.0/4.0, label="1/4")
G.add_edge(0, 3, weight=1.0/4.0, label="1/4")
G.add_edge(0, 4, weight=1.0/4.0, label="1/4")


G.add_edge(1, 2, weight=1.0/3.0, label="1/3")
G.add_edge(1, 3, weight=1.0/3.0, label="1/3")
G.add_edge(1, 4, weight=1.0/3.0, label="1/3")

G.add_edge(2, 0, weight=1.0/2.0, label="1/2")
G.add_edge(2, 1, weight=1.0/2.0, label="1/2")

G.add_edge(3, 0, weight=1.0/2.0, label="1/2")
G.add_edge(3, 2, weight=1.0/2.0, label="1/2")

G.add_edge(4, 1, weight=1.0/3.0, label="1/3")
G.add_edge(4, 2, weight=1.0/3.0, label="1/3")
G.add_edge(4, 3, weight=1.0/3.0, label="1/3")

print(nx.__version__)

# To plot graph, convert to a PyGraphviz graph for drawing
Ag = nx.nx_agraph.to_agraph(G)
Ag.layout(prog='dot')
Ag.draw('web.svg')
from IPython.display import SVG
SVG('web.svg')


1.11
Out[4]:
%3 0 p0 1 p1 0->1 1/4 2 p2 0->2 1/4 3 p3 0->3 1/4 4 p4 0->4 1/4 1->2 1/3 1->3 1/3 1->4 1/3 2->0 1/2 2->1 1/2 3->0 1/2 3->2 1/2 4->1 1/3 4->2 1/3 4->3 1/3

In [ ]: