2

我一直被一些非常简单的事情困扰。我正在尝试绘制并显示具有 2 个节点和 1 条边的图形。但我得到这个错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 133, in      draw
draw_networkx(G,pos=pos,ax=ax,**kwds)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 267, 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 546, in draw_networkx_edges
ax.add_collection(edge_collection)
File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1445, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "/usr/local/lib/python2.7/site-packages/matplotlib/collections.py", line 167, in get_datalim
offsets.shape = (-1, 2)                     # Make it Nx2
AttributeError: incompatible shape for a non-contiguous array

这很简单,我只需添加 2 个节点

add_node(1) 
add_node(2)
add_edge(1,2)

然后画出来并展示出来。但我就是不能让它工作。

4

2 回答 2

3

这是一个有效的解决方案。

import matplotlib.pyplot as plt
import networkx as nx

# Create a basic Graph
g=nx.Graph()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2)

# Output the result
nx.draw(g)
plt.show()
于 2014-05-09T08:57:14.650 回答
2
import networkx as nx
g=nx.Graph()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2)
nx.draw(g)
于 2012-10-28T21:56:06.197 回答