Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkXError: Node 8 has no position

I just started programming and recently I'm working in ipython notebook with networkx. This code below is working perfectly if you run it, but if you uncomment #G.add_edge(2, 8, egdes=6) it gives you the error NetworkXError: Node 8 has no position. Why does it only work until the sixth node?

import networkx as nx 
import matplotlib.pyplot as plt
import pylab
%matplotlib inline

pos=nx.spring_layout(G)

G = nx.Graph()

G.add_edge(1, 2, egdes=1)
G.add_edge(1, 3, egdes=2)
G.add_edge(1, 4, egdes=3)
G.add_edge(1, 5, egdes=4)
G.add_edge(1, 6, egdes=5)
#G.add_edge(2, 8, egdes=6)

nx.draw(G,pos)
edge_labels=dict([((fe,se,),e['egdes'])
            for fe,se,e in G.edges(data=True)])

nx.draw_networkx_edge_labels(G,pos,edge_labels)

pylab.show()

I hope one of you guys can help me, thanks in advance!

like image 294
Anna Tol Avatar asked Nov 13 '14 13:11

Anna Tol


1 Answers

You need to create the node positions

pos=nx.spring_layout(G)

after you have built the graph (added all edges and nodes) and before you draw it.

like image 166
Aric Avatar answered Sep 22 '22 02:09

Aric