networkx: nx.draw is not drawing

Viewed 900

I am running a simple python script on my shell. In the script I create a simple graph(2 nodes, 1 edge) and trying to draw it to my screen.

I do not get any error, but the graph is not showing

import matplotlib
import networkx as nx

G = nx.DiGraph()
G.add_node(1)
G.add_node(2)
G.add_edge(1, 2)
nx.draw(G)

I don't get any error but there is no output.

Can you help?

2 Answers

Maybe try

import matplotlib.pyplot as plt 
import networkx as nx

G = nx.DiGraph()
G.add_node(1)
G.add_node(2)
G.add_edge(1, 2)
nx.draw(G)
plt.draw()

this works for me

I needed to add

import pylab
pylab.show()

as followed:

import pylab
import matplotlib 
import networkx as nx

G = nx.DiGraph()
G.add_node(1)
G.add_node(2)
G.add_edge(1, 2)
nx.draw(G)
pylab.show()
Related