为什么我画图遇到除零误差?

问题描述:

我有这样一些数据:为什么我画图遇到除零误差?

4556 5092 0.7000 
4556 4785 0.7500 
4556 5397 0.7000 
4556 5139 0.7500 
4556 5937 0.8333 
4556 6220 0.7000 
4556 5139 0.7500 
4556 6220 0.7063 
4559 4563 0.7500 
4559 4770 0.7500 
4559 4837 0.7500 
4559 5640 0.7500 
4559 4563 0.7500 
4559 4770 0.7500 
4559 4837 0.7500 
4559 5640 0.7500 
4561 4607 1.0000 
4561 4600 0.7500 
4561 4562 0.7500 
4561 5090 0.7500 
4561 5197 1.0000 
4561 5182 0.7500 
4561 5937 0.7500 
4561 6143 0.7500 
4561 5632 1.0000 
4561 5870 1.0000 
4561 6220 0.6756 
4561 6143 0.7500 
4561 6220 0.6750 
4562 4607 0.7500 
4562 5197 0.7500 
4562 5632 0.7500 
4562 5870 0.7500 
4562 6220 0.6656 
4564 4837 0.7500 
4564 4837 0.7500 
4566 5805 0.8750 
4566 5981 0.6729 
4571 4919 0.7000 
4571 6004 0.7500 
4571 6089 0.7000 
4571 6044 0.7500 
4571 6041 0.7000 
4571 5863 0.7500 
4571 6089 0.6398 

前两个科拉姆是节点,而第三个是边缘的长度。

我的代码是:

import matplotlib.pyplot as plt 
import networkx as nx 
G=nx.Graph() 
filedata = file("1.txt") 
for line in filedata: 
    datas = line.split() 
    G.add_edge(datas[0],datas[1],length=datas[2]) 

#nx.draw(G,with_lables=False,node_size=100) 
nx.draw(G,with_lables=False,node_size=100) 
plt.savefig("data.png") 
#plt.show() 

但我encouter:

/usr/local/lib/python2.7/dist-packages/networkx-1.7-py2.7.egg/networkx/drawing/layout.py:514: RuntimeWarning: divide by zero encountered in int_scalars 
    pos[:,i]*=scale/lim 

有什么错networkx?

+5

我没有使用任何你正在使用的库,但我能想到的第一件事是数据[2]可能被铸造成一个int,这将使其成为0.尝试显式地将其转换为浮点数。 – pypat 2013-05-07 08:04:44

+0

你确定数据[0],数据[1]和数据[2]确实包含浮点数吗?而不是字符串? – 2013-05-07 08:55:07

+0

https://groups.google.com/forum/?fromgroups=#!topic/networkx-discuss/DvwOq62sUnk这是因为networkx – young001 2013-05-07 09:04:51

我没有在我的系统上得到任何错误。但经常遇到这个问题。在此警告之后,networkx在绘图中心绘制一条粗黑线并保存。我升级到了Networkx 1.8,到目前为止还没有遇到这个错误。