Created
January 16, 2013 06:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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