如何解决IndexError:在wxPython中,索引(x)超出了轴0的大小(y)的范围?

问题描述 投票:0回答:1

[我在这里尝试显示下巴的动画。运行代码会在调用OnClicked()事件时出现错误。

def OnClicked(self, e):
    print("ok")
    self.ani = animation.FuncAnimation(self.figure_animation, self.animate , init_func=self.init, interval=0.1,
                                       blit=True)

def init(self):  # only required for blitting to give a clean slate.
    x = self.mat_px[0]
    y = self.mat_py[0]
    self.jaw_outline.set_data(x, y)
    return self.img, self.jaw_outline

def animate(self, i):
    # update the data
    x = self.mat_px[i]
    y = self.mat_py[i]
    self.jaw_outline.set_data(x, y)
    poa = self.axes_animation.scatter(self.h2_POA_pos[i], self.K2_POA_pos[i], color='red', s=150)
    jaw_area_fill = self.axes_animation.fill_between(x, y, 0, facecolor=[(254 / 255, 157 / 255, 111 / 255)])

    return self.img, self.jaw_outline, jaw_area_fill, poa

考虑一下,如果self.mat_px的长度为80,那么我在pycharm控制台中连续出现此“索引80超出轴80尺寸80的范围”的错误。我收到如下所示的不间断错误。

  • IndexError:索引80超出了大小为75的轴0的边界。
  • [IndexError:索引81超出轴75的大小75。
  • [IndexError:索引82超出大小为75的轴0的边界。
  • 依此类推

并且如果我将blit设置为False,那么我不会收到任何错误,但这样做不会给我正确的结果。请帮助我解决此问题。

python animation wxpython
1个回答
0
投票

我建议使用%(模)运算符(请参见Binary arithmetic operations),以将索引分别限制在self.mat_px的范围内self.mat_py

x = self.mat_px[i % len(self.mat_px)]
y = self.mat_py[i % len(self.mat_py)]
© www.soinside.com 2019 - 2024. All rights reserved.