【问题标题】:OverflowError: Python int too large to convert to C ssize_t溢出错误:Python int 太大而无法转换为 C ssize_t
【发布时间】:2016-11-22 21:20:58
【问题描述】:

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

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

【问题讨论】:

  • 代码是从我的程序中提取的,但是程序运行良好。
  • 此代码目前不会为我引发错误。无论哪种方式,请在收到错误时发布您收到的回溯消息。
  • 我添加了 Traceback。
  • 你做了,但错误仍然无法重现。

标签: python python-3.x matplotlib networkx


【解决方案1】:

似乎问题出在 TkAgg 后端,我尝试添加:

import matplotlib
matplotlib.use('Qt5Agg')

之前

import matplotlib.pyplot as plt

没想到一切正常。

【讨论】:

  • 这就是我们请求minimal reproducible example 的原因。人们阅读您的代码并思考问题需要时间和精力。您应该尽可能确保这样做的人至少使用了重现问题的代码。
  • 代码似乎无法为您重现问题,您知道代码通常可以正常工作。也许这是一个 matplotlib 错误。
猜你喜欢
  • 2021-01-22
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 2021-04-25
  • 2014-01-08
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多