2016-11-22 473 views
0

今天我碰到這個錯誤,代碼:OverflowError:Python的INT太大,轉換爲C ssize_t供

import networkx as nx 
import matplotlib.pyplot as plt 

l = [ 
    1, 7, 0, 0, 6, 8, 1, 8, 0, 5, 4, 2, 2, 8, 8, 4, 5, 4, 8, 3, 1, 8, 0, 1, 9, 
    7, 5, 4, 5, 4, 2, 4, 5, 8, 1, 4, 4, 5, 7, 4, 5, 2, 4, 2, 4, 7, 8, 5, 0 
] 

plt.figure(15, figsize=(15, 7)) 
pos = {} 

for i in range(0, len(l)): 
    pos[i] = (i, l[i]) 

X = nx.Graph() 
X.add_nodes_from(pos.keys()) 

for i in range(0, len(l) - 1): 
    X.add_edge(i, i + 1, weight=1, label='I') 

for n1, p in pos.items(): 
    X.node[n1]['pos'] = p 

labeldict = dict(zip(range(0, len(l)), [str(i) for i in l])) 
nx.draw_networkx_nodes(X, pos, node_size=200, node_color='orange') 
nx.draw_networkx_labels(
    X, pos, labels=labeldict, font_size=14, font_family='sans-serif') 
nx.draw_networkx_edges(X, pos, width=1, edge_color='g') 
plt.show() 

但我從來沒有遇到這樣的錯誤有類似的代碼befor,可能有人告訴我怎麼來的錯誤?

完整的錯誤是:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3/dist-packages/matplotlib/backends/tkagg.py", line 22, in blit 
    id(data), colormode, id(bbox_array)) 
_tkinter.TclError: invalid command name "PyAggImagePhoto" 

在處理上述異常,另一個異常:

Traceback (most recent call last): 
    File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__ 
    return self.func(*args) 
    File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 283, in resize 
    self.show() 
    File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 355, in draw 
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2) 
    File "/usr/lib/python3/dist-packages/matplotlib/backends/tkagg.py", line 26, in blit 
    _tkagg.tkinit(tk.interpaddr(), 1) 
OverflowError: Python int too large to convert to C ssize_t 
+0

代碼是從我的程序中提取的,但程序運行良好。 – Tom

+0

這段代碼目前不會引起我的錯誤。無論哪種方式,請在發生錯誤時發佈您收到的追溯消息。 –

+0

我添加了Traceback。 – Tom

回答

0

看來我們走來了TkAgg後端的問題,我試着添加:

import matplotlib 
matplotlib.use('Qt5Agg') 

之前

import matplotlib.pyplot as plt 

意外的所有工作正常。

+0

這就是我們要求[mcve]的原因。人們需要時間和精力來閱讀代碼並思考問題。你應該儘可能地做,以確保這樣做的人至少使用代碼來重現問題。 – Joel

+0

看來代碼不會爲您再現問題,您知道代碼一般工作。也許這是一個matplotlib錯誤。 – Tom