How to represent graphs with IPython

You can use networkx and, if you need to render the graph in ipython notebook, nxpd

import networkx as nx
from nxpd import draw
G = nx.DiGraph()
G.graph['dpi'] = 120
G.add_nodes_from(range(1,9))
G.add_edges_from([(1,2),(1,3),(2,4),(3,6),(4,5),(4,6),(5,7),(5,8)])
draw(G, show='ipynb')

enter image description here


You can use pygraphviz:

import pygraphviz

G = pygraphviz.AGraph(directed=True)
G.add_nodes_from(range(1,9))
G.add_edges_from([(1,2),(1,3),(2,4),(3,6),(4,5),(4,6),(5,7),(5,8)])
G.layout()
G.draw('graph.png')

Then in a markdown block:

![graph](graph.png)

Which renders to:

graph