【问题标题】:How to resolve IndexError: index (x) is out of bounds for axis 0 with size (y) in wxPython?如何解决 IndexError: index (x) is out of bounds for axis 0 with size (y) in wxPython?
【发布时间】:2020-05-22 04:15:19
【问题描述】:

我在这里尝试显示下颌线的动画。运行代码会在调用 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 控制台中不断收到“index 80 is out of bounds for axis 0 with size 80”错误。我收到类似下面的不间断错误。

  • IndexError:索引 80 超出轴 0 的范围,大小为 75。
  • IndexError:索引 81 超出轴 0 的范围,大小为 75。
  • IndexError:索引 82 超出轴 0 的范围,大小为 75。
  • 等等

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

【问题讨论】:

    标签: python animation wxpython


    【解决方案1】:

    我建议使用%(取模)运算符(请参阅Binary arithmetic operations),将索引限制在self.mat_pxself.mat_py 的范围内:

    x = self.mat_px[i % len(self.mat_px)]
    y = self.mat_py[i % len(self.mat_py)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-09
      • 2021-12-17
      • 1970-01-01
      • 2020-03-13
      • 2019-09-21
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多