Skip to content

Instantly share code, notes, and snippets.

@tacaswell
Created January 16, 2013 06:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacaswell/4545013 to your computer and use it in GitHub Desktop.
Save tacaswell/4545013 to your computer and use it in GitHub Desktop.
A quick class for binding keys to step through an iterable in matplotlib
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment