Reputation: 582
I have the following code which works for a random graph as can be shown. However when I try to use other graph types I get an error in the edge drawing function. In particular the edge positions.
If you comment
G = nw.random_geometric_graph(200, 0.125)
and un-comment
G = nw.barabasi_albert_graph(200, 2)
Error messages appear. I am new to python and NetworkX
in particular so any help is appreciated!
import matplotlib.pyplot as plt
import networkx as nw
G = nw.random_geometric_graph(200, 0.125)
#G = nw.watts_strogatz_graph(200, 3, 0.125, seed=None)
#G = nw.barabasi_albert_graph(200, 2)
# position is stored as node attribute data for random_geometric_graph
pos = nw.get_node_attributes(G, 'pos')
# find node near center (0.5, 0.5)
dmin = 1
ncenter = 0
for n in pos:
x, y = pos[n]
d = (x - 0.5) ** 2 + (y - 0.5) ** 2
if d < dmin:
ncenter = n
dmin = d
# color by path length from node near center
p = nw.single_source_shortest_path_length(G, ncenter)
plt.figure(figsize=(8, 8))
nw.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
nw.draw_networkx_nodes(G, pos, nodelist=list(p.keys()), node_size=80, node_color=list(p.values()), cmap=plt.cm.Reds_r)
plt.xlim(-0.05, 1.05)
plt.ylim(-0.05, 1.05)
plt.axis('off')
plt.savefig('random_geometric_graph.png')
plt.show()
The error message given is;
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-11-> in <module>()
22 plt.figure(figsize=(8,8))
23
---> 24 nw.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
25 nw.draw_networkx_nodes(G, pos, nodelist=list(p.keys()), node_size=80, node_color=list(p.values()), cmap=plt.cm.Reds_r)
26
/Users//anaconda/lib/python3.6/site-packages/networkx/drawing/nx_pylab.py in draw_networkx_edges(G, pos, edgelist, width, edge_color, style, alpha, edge_cmap, edge_vmin, edge_vmax, ax, arrows, label, **kwds)
513
514 # set edge positions
--> 515 edge_pos = numpy.asarray([(pos[e[0]], pos[e[1]]) for e in edgelist])
516
517 if not cb.iterable(width):
/Users//anaconda/lib/python3.6/site-packages/networkx/drawing/nx_pylab.py in <listcomp>(.0)
513
514 # set edge positions
--> 515 edge_pos = numpy.asarray([(pos[e[0]], pos[e[1]]) for e in edgelist])
516
517 if not cb.iterable(width):
KeyError: 0
Upvotes: 3
Views: 4414
Reputation: 28356
You can see that the problem (from stack trace) is in this line:
nw.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
And the error is KeyError
, so something cannot be found. Probably, you need to draw edges
here, but you do provide the nodelist
. According official docs, method to drawing the edges
should accept edgelist
, not a nodelist
.
So you need to do this:
nw.draw_networkx_edges(G, pos, edgelist=[SOME_EDGES_HERE], alpha=0.4)
Note that this should be edges, not nodes, so you need to find them from the center node.
Upvotes: 2
Reputation: 13041
I don't think any other graph initialisation methods apart from random_geometric_graph
sets the node position automatically (as the connectivity in this graph depends on the node positions, it makes sense to set one by default). If you check your example with the watts_strogatz_graph
, the dictionary that is returned is actually empty (although it arguably should throw a KeyError
).
You need to determine the layout explicitly, e.g. using
pos = nw.spring_layout(G)
or any of the other layout algorithms.
Upvotes: 4