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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With