Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to fix int not subscriptable

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from matplotlib.patches import Rectangle

>Solution :

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 zipping sub_x and sub_y together gives you x, y tuples in exactly the form that you need them for the Rectangle constructor.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading