From a5fccbc7bf02c20401e405cb9a8eeec7c7afd47c Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Wed, 1 Oct 2014 22:37:42 -0400 Subject: [PATCH 1/2] Turn off blitting and improve RadioButton --- examples/animation/animate_decay.py | 4 ++-- lib/matplotlib/widgets.py | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/animation/animate_decay.py b/examples/animation/animate_decay.py index 589af36f7e8d..71f1eb5d341a 100644 --- a/examples/animation/animate_decay.py +++ b/examples/animation/animate_decay.py @@ -7,7 +7,7 @@ def data_gen(t=0): cnt = 0 while cnt < 1000: cnt += 1 - t += 0.05 + t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) fig, ax = plt.subplots() @@ -32,6 +32,6 @@ def run(data): return line, -ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10, +ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, repeat=False) plt.show() diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 6675903b6877..cf7447334d58 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -698,17 +698,30 @@ def inside(p): pcirc = np.array([p.center[0], p.center[1]]) return dist(pclicked, pcirc) < p.radius - for p, t in zip(self.circles, self.labels): + for i, (p, t) in enumerate(zip(self.circles, self.labels)): if t.get_window_extent().contains(event.x, event.y) or inside(p): - inp = p - thist = t - self.value_selected = t.get_text() + index_match = i break else: return - for p in self.circles: - if p == inp: + self.set_active(index_match) + + def set_active(self, index): + """ + Trigger which radio button to make active. + + *index* is an index into the original label list + that this object was constructed with. + + """ + if 0 > index >= len(self.labels): + raise ValueError("Invalid RadioButton index: %d" % index) + + self.value_selected = self.labels[index].get_text() + + for i, p in enumerate(self.circles): + if i == index: color = self.activecolor else: color = self.ax.get_axis_bgcolor() @@ -720,7 +733,7 @@ def inside(p): if not self.eventson: return for cid, func in six.iteritems(self.observers): - func(thist.get_text()) + func(self.labels[index].get_text()) def on_clicked(self, func): """ From e012f780f16080afa562f9925ea745f01e4eb6f4 Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Sat, 25 Oct 2014 14:08:36 -0400 Subject: [PATCH 2/2] Added a set_active() for CheckButtons --- lib/matplotlib/widgets.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index cf7447334d58..46aba361a48d 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -569,24 +569,39 @@ def _clicked(self, event): if event.inaxes != self.ax: return - for p, t, lines in zip(self.rectangles, self.labels, self.lines): + for i, (p, t) in enumerate(zip(self.rectangles, self.labels)): if (t.get_window_extent().contains(event.x, event.y) or p.get_window_extent().contains(event.x, event.y)): - l1, l2 = lines - l1.set_visible(not l1.get_visible()) - l2.set_visible(not l2.get_visible()) - thist = t + self.set_active(i) break else: return + def set_active(self, index): + """ + Directly (de)activate a check button by index. + + *index* is an index into the original label list + that this object was constructed with. + Raises ValueError if *index* is invalid. + + Callbacks will be triggered if :attr:`eventson` is True. + + """ + if 0 > index >= len(self.labels): + raise ValueError("Invalid CheckButton index: %d" % index) + + l1, l2 = self.lines[index] + l1.set_visible(not l1.get_visible()) + l2.set_visible(not l2.get_visible()) + if self.drawon: self.ax.figure.canvas.draw() if not self.eventson: return for cid, func in six.iteritems(self.observers): - func(thist.get_text()) + func(self.labels[index].get_text()) def on_clicked(self, func): """ @@ -700,19 +715,20 @@ def inside(p): for i, (p, t) in enumerate(zip(self.circles, self.labels)): if t.get_window_extent().contains(event.x, event.y) or inside(p): - index_match = i + self.set_active(i) break else: return - self.set_active(index_match) - def set_active(self, index): """ Trigger which radio button to make active. *index* is an index into the original label list that this object was constructed with. + Raise ValueError if the index is invalid. + + Callbacks will be triggered if :attr:`eventson` is True. """ if 0 > index >= len(self.labels):