How to represent graphs using ipython

I recently discovered ipython notebook

which is a powerful tool. As an IT student, I was looking for expectations for graphing in Python. For example, I would like to know if there is a library (like numpy

or matplotlib

?) That can draw from this

{ "1" : ["3", "2"],
  "2" : ["4"],
  "3" : ["6"],
  "4" : ["6"],
  "5" : ["7", "8"],
  "6" : [],
  "7" : [],
  "8" : []
}

      

something like that:

(source: developpez.com)

Is there something like this?

+3


source to share


3 answers


You can use networkx and if you need to display 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

+5


source


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 the markdown block:



![graph](graph.png)

      

What concerns:

graph

+2


source


If you also want to animate graphs (for example, to learn how the algorithm works) you can use https://github.com/mapio/GraphvizAnim

0


source







All Articles