public
Created

A quick class for binding keys to step through an iterable in matplotlib

  • Download Gist
push_to_advance.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
class push_to_advance(object):
def __init__(self):
self.fig = plt.figure()
self.ax = self.fig.gca()
self.bound_keys = []
self.bound_cid = {}
 
def add_step_through(self, gen, key):
key = key[0] # make a single char
if key in self.bound_keys:
raise RuntimeError("key %s already bound"%key)
first_data = gen.next()
self.ax.plot(first_data)
self.fig.canvas.draw()
self.bound_keys.append(key)
def ontype(event):
if event.key == key:
try:
self.ax.plot(gen.next())
self.fig.canvas.draw()
except StopIteration:
self.fig.canvas.mpl_disconnect(self.bound_cid[key])
del self.bound_cid[key]
self.bound_keys.remove(key)
 
self.bound_cid[key] = self.fig.canvas.mpl_connect('key_press_event', ontype))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.