python - Where do timers live in matplotlib -
i using matplotlib timer objects register own update function animation. can't seem stop callbacks once start though without keeping reference timer object.
it has been experience far when create object in matplotlib given reference added list inside other object (axis in figures, lines in axis etc.) can queried later. cannot find timer objects live however. problem can summarized code snippet
import matplotlib.pyplot plt import numpy 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()
run above code snippet, try programmatically stop flashing. function needs called
timer.remove_callback(update).
to clear. know can keep reference timer object , problem goes away. looking explanation of object must living in matplotlib.
how bout
self.timer = fig.canvas.new_timer(interval=100) ... self.timer.remove_callback(...)
to clarify reference in callafter method. there no reference stored in figure or canvas can see in backend source
def new_timer(self, *args, **kwargs): """ creates new backend-specific subclass of :class:`backend_bases.timer`. useful getting periodic events through backend's native event loop. implemented backends guis. optional arguments: *interval* timer interval in milliseconds *callbacks* sequence of (func, args, kwargs) func(*args, **kwargs) executed timer every *interval*. """ return timertk(self._tkcanvas, *args, **kwargs)
which returns timertk instance. reference continues live because in timertk.start()
method see callafter continues keep timer garbage collecting
class timertk(timerbase): ... def _timer_start(self): self._timer_stop() self._timer = self.parent.after(self._interval, self._on_timer)
and why every example shows saving own reference timer
Comments
Post a Comment