Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw graph in NetworkX

I'm trying to draw any graph in NetworkX, but get nothing, not even errors:

import networkx as nx import matplotlib.pyplot as plt g1=nx.petersen_graph() nx.draw(g1) 
like image 261
denfromufa Avatar asked Oct 06 '13 19:10

denfromufa


2 Answers

Add to the end:

plt.show() 

import networkx as nx import matplotlib.pyplot as plt g1 = nx.petersen_graph() nx.draw(g1) plt.show() 

When run from an interactive shell where plt.ion() has been called, the plt.show() is not needed. This is probably why it is omitted in a lot of examples.

If you run these commands from a script (where plt.ion() has not been called), the plt.show() is needed. plt.ion() is okay for interactive sessions, but is not recommended for scripts.

like image 118
unutbu Avatar answered Oct 02 '22 15:10

unutbu


in ipython notebook, just type in magic

%matplotlib inline 

or

%matplotlib notebook 
like image 33
denfromufa Avatar answered Oct 02 '22 14:10

denfromufa