Python 3.x 如何防止init在matplotlib中重新初始化

Python 3.x 如何防止init在matplotlib中重新初始化,python-3.x,matplotlib,Python 3.x,Matplotlib,我正在使用matplotlib生成带有键盘输入的动画。通过按键盘,它在屏幕上移动一个圆圈。我的问题是matplotlib的init部分会重新运行并重置以前的值 def init(): patch.center = (0, 0) print("this section reruns") ax.add_patch(patch) return patch, def animate(i): while (True): global x, y, k x, y = patch.cen

我正在使用matplotlib生成带有键盘输入的动画。通过按键盘,它在屏幕上移动一个圆圈。我的问题是matplotlib的init部分会重新运行并重置以前的值

def init():
patch.center = (0, 0)
print("this section reruns")
ax.add_patch(patch)
return patch,


def animate(i):
  while (True):
    global x, y, k
    x, y = patch.center
    [x,y] = getData()
    patch.center = (x, y)
    return patch,

def showAnimation():
  anim = animation.FuncAnimation(fig, animate,
                               init_func=init,
                               frames=360,
                               interval=20,
                               blit=True)
  plt.show()
  return anim

try:
  lis = keyboard.Listener(on_press=on_press)
  lis.start() # start to listen on a separate thread

except:
  print("error")

while 1:
  test1 = showAnimation()
任何建议都将受到重视 谢谢你

这部分:

while 1:
  test1 = showAnimation()
每次从一开始就启动动画,并执行init()函数

不要在
while 1
循环中运行动画,只需在没有循环的情况下运行动画:

test1 = showAnimation()