i'm currently working with two arrays that contain coordinates respectively. I want to create a Rectangle with the values of these coordinates (this is only a snippet of the code but it's where the problem seems to be):
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)
I'm getting "TypeError: 'int' object is not subscriptable" in the line with the declaration of the rectangle. I created all of the auxiliary variables to be extra sure that i wasn't subscripting the int. Can anyone tell what's going on?
Edit: Full traceback: " File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/patches.py", line 728, in init self._x0 = xy[0] TypeError: 'int' object is not subscriptable" Rectangle is imported from matplotlib like so:
from matplotlib.patches import Rectangle
CodePudding user response:
Per the matplotlib doc: https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html
the xy
parameter to Rectangle
should be a tuple, not two different parameters; that's the thing that's being subscripted (inside the constructor). This should work:
for xy in zip(sub_x, sub_y):
ax.add_patch(Rectangle(xy, 1, 1))
Note that zip
ping sub_x
and sub_y
together gives you x, y
tuples in exactly the form that you need them for the Rectangle
constructor.