我目前正在使用兩個分別包含坐標的陣列。我想用這些坐標的值創建一個 Rectangle (這只是代碼的一個片段,但它似乎是問題所在):
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)
我在矩形宣告的行中收到“TypeError:'int' object is not subscriptable”。我創建了所有輔助變數,以確保我沒有為 int 下標。誰能告訴發生了什么?
編輯:完整追溯:“檔案”/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/patches.py”,第 728 行,在init self._x0 = xy[0 ] TypeError: 'int' object is not subscriptable" 矩形是從 matplotlib 匯入的,如下所示:
from matplotlib.patches import Rectangle
uj5u.com熱心網友回復:
根據 matplotlib 檔案:https ://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html
xy引數 toRectangle應該是一個元組,而不是兩個不同的引數;這就是被下標的東西(在建構式內部)。這應該有效:
for xy in zip(sub_x, sub_y):
ax.add_patch(Rectangle(xy, 1, 1))
請注意,zippingsub_x和sub_ytogether以建構式x, y所需的形式為您提供元組。Rectangle
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451514.html
標籤:Python matplotlib
