如何修复 int 不可订阅

how to fix int not subscriptable

我目前正在使用两个分别包含坐标的数组。我想用这些坐标的值创建一个矩形(这只是代码的一个片段,但它似乎是问题所在):

for j in range(0, len(sub_x)):
        aux_x = sub_x[j]
        aux_y = sub_y[j]
        int_x = int(aux_x)
        int_y = int(aux_y)
        print("ints: ", int_x, int_y)
        rectangle = Rectangle(int_x, int_y,1,1)
        ax.add_patch(rectangle)

我在矩形声明的行中收到“类型错误:'int' 对象不可订阅”。我创建了所有辅助变量以确保我没有下标 int。谁能告诉我这是怎么回事?

编辑:完整回溯: “文件”/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/patches.py,第 728 行,在 init 中 self._x0 = xy[0] 类型错误:'int' 对象不可订阅” 矩形是从 matplotlib 导入的,如下所示:

from matplotlib.patches import Rectangle

根据 matplotlib 文档:https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html

Rectanglexy 参数应该是一个元组,而不是两个不同的参数;那就是被下标的东西(在构造函数中)。这应该有效:

for xy in zip(sub_x, sub_y):
    ax.add_patch(Rectangle(xy, 1, 1))

请注意,zipping sub_xsub_y 一起为您提供 x, y 元组,其形式与 Rectangle 构造函数所需的形式完全相同。