Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent graphs with IPython

Recently I discovered IPython notebook which is a powerful tool. As an IT student, I was looking for a way to represent graphs in Python. For example, I would like to know if there's a library (like numpy or matplotlib ?) which can draw from this

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

something like this :

(source : developpez.com)

Is there something like this ?

like image 715
FunkySayu Avatar asked Apr 21 '15 13:04

FunkySayu


People also ask

How do I display an image in IPython?

We could use the Image class of IPython. display to load and display a local image in the IPython notebook. Here, I have used the Image( ) function, where one needs to supply the filename i.e., the location path for the image file. Additionally, we can vary the width and height to adjust the image size.

How do you plot a graph in Python Jupyter?

Simple PlotThe first line imports the pyplot graphing library from the matplotlib API. The third and fourth lines define the x and y axes respectively. The plot() method is called to plot the graph. The show() method is then used to display the graph.


1 Answers

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

like image 72
AGS Avatar answered Oct 11 '22 06:10

AGS