OverflowError:Python int太大而无法转换为C ssize_t

时间:2016-11-22 10:41:21

标签: python python-3.x matplotlib networkx

我今天遇到这个错误,代码是:

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()

但我从来没有遇到类似代码的错误,有人能告诉我怎么会出错?

完整错误是:

 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

1 个答案:

答案 0 :(得分:0)

似乎问题来自TkAgg后端,我尝试添加:

import matplotlib
matplotlib.use('Qt5Agg')

之前

import matplotlib.pyplot as plt

意外的全部工作正常。