我正在使用 matplotlib 计时器对象来注册我自己的动画更新函数。一旦回调开始,我似乎无法停止回调,但不保留对计时器对象的引用。
到目前为止,我的经验是,当我在 matplotlib 中创建一个对象时,我得到了对它的引用,但它也被添加到其他对象(图中的轴,轴中的线等)内的列表中,然后可以查询之后。但是,我找不到计时器对象的位置。我的问题可以用这个代码片段来概括
import matplotlib.pyplot as plt
import numpy as np
def update():
plt.get_current_fig_manager().canvas.figure.patch.set_facecolor(str(np.random.random()))
plt.draw()
def start_animation():
timer = fig.canvas.new_timer(interval = 50)
timer.add_callback(update)
timer.start()
fig = plt.figure()
start_animation()
运行上面的代码片段,然后尝试以编程方式停止闪烁。需要调用的函数是
timer.remove_callback(update).
要清楚。我知道我可以只保留对计时器对象的引用,这个问题就消失了。我正在寻找这个对象必须在 matplotlib 中的位置的解释。