python - Pyplot connect to timer event? -
the same way have plt.connect('button_press_event', self.on_click)
have plt.connect('each_five_seconds_event', self.on_timer)
how can achieve in way that's similar i've shown above?
edit: tried
fig = plt.subplot2grid((num_cols, num_rows), (col, row), rowspan=rowspan, colspan=colspan) timer = fig.canvas.new_timer(interval=100, callbacks=[(self.on_click)]) timer.start()
and got
attributeerror: 'axessubplot' object has no attribute 'canvas'
also,
new_timer(interval=100, callbacks=[(self.on_click)])
good, or have pass more stuff in there, in example?
matplotlib has backend-agnostic timer integrates gui's event loop. have @ figure.canvas.new_timer(...)
.
the call signature touch akward, works. (you need explicitly specify empty sequences , dicts if call function(s) don't take arguments or kwargs.)
as minimal example:
import matplotlib.pyplot plt def on_timer(): print 'hi!' fig, ax = plt.subplots() # interval in milliseconds. # "callbacks" expects sequence of (func, args, kwargs) timer = fig.canvas.new_timer(interval=5000, callbacks=[(on_timer, [], {})]) timer.start() plt.show()
and "fancier" example animates 2d brownian walk:
import numpy np import matplotlib.pyplot plt def on_timer(line, x, y): x.append(x[-1] + np.random.normal(0, 1)) y.append(y[-1] + np.random.normal(0, 1)) line.set_data(x, y) line.axes.relim() line.axes.autoscale_view() line.axes.figure.canvas.draw() x, y = [np.random.normal(0, 1)], [np.random.normal(0, 1)] fig, ax = plt.subplots() line, = ax.plot(x, y, color='aqua', marker='o') timer = fig.canvas.new_timer(interval=100, callbacks=[(on_timer, [line, x, y], {})]) timer.start() plt.show()
Comments
Post a Comment