我正在尝试将 NetworkX 用于 Python,到目前为止它还不错,但我坚持绘图。
我的数据实际上是一个城市十字路口的列表。节点是 X、Y(纬度、经度)的十字路口,边应该是道路。到目前为止这么简单。
所以我设法用固定的位置画出我的小城市:
G = nx.Graph()
for node in nodes:
attributes = dict(node.items())
G.add_node(attributes.pop('id'))
G.node[node.get('id')]['pos'] = attributes.values()
pos = nx.get_node_attributes(G, 'pos')
nx.draw(G,pos)
plt.show()
它看起来像这样:
就像我想要的那样,但我需要添加边缘。因此,如果我像这样添加它们:
G = nx.Graph()
for node in nodes:
attributes = dict(node.items())
G.add_node(attributes.pop('id'))
G.node[node.get('id')]['pos'] = attributes.values()
for link in links:
attributes = dict(link.items())
G.add_edge(attributes.pop('from'), attributes.pop('to'))
pos = nx.get_node_attributes(G, 'pos')
nx.draw(G,pos)
plt.show()
我收到这个可怕的错误:
Traceback (most recent call last):
File "ms/Main.py", line 28, in <module>
nx.draw(G,pos)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 131, in draw
draw_networkx(G, pos=pos, ax=ax, **kwds)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 265, in draw_networkx
edge_collection = draw_networkx_edges(G, pos, **kwds)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 610, in draw_networkx_edges
minx = numpy.amin(numpy.ravel(edge_pos[:, :, 0]))
File "/usr/local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2216, in amin
out=out, keepdims=keepdims)
File "/usr/local/lib/python2.7/site-packages/numpy/core/_methods.py", line 29, in _amin
return umr_minimum(a, axis, None, out, keepdims)
TypeError: cannot perform reduce with flexible type
如果我不设置固定位置,我可以画它没有问题,但我非常需要边缘是静态的。有什么建议吗?非常感谢!