I've been working on an application that uses matplotlib as its
plotting library, and I've been noticing a steady decrease in
performance over time. I figured out the cause as coming from the
matplotlib SpanSelector widget - I had to create a new widget every
time the axes were cleared, as the SpanSelector would not draw
correctly afterwards. The event processing calls were still being
made though, so over time I collected hundreds of SpanSelectors,
rendering the app unusable after a while. This patch adds a new_axes
method to the SpanSelector that can be called to simply re-assign the
spanselector to a new axes so I only need one. Below is a diff
against the latest svn.
Also, is it better practice to submit patches like this to this
mailing list, or the sourceforge tracker? I've seen some people do
both, and it seems like the tracker isn't kept up very well...
Index: lib/matplotlib/widgets.py
···
===================================================================
--- lib/matplotlib/widgets.py (revision 4950)
+++ lib/matplotlib/widgets.py (working copy)
@@ -829,12 +829,9 @@
self.ax = ax
self.visible = True
- self.canvas = ax.figure.canvas
- self.canvas.mpl_connect('motion_notify_event', self.onmove)
- self.canvas.mpl_connect('button_press_event', self.press)
- self.canvas.mpl_connect('button_release_event', self.release)
- self.canvas.mpl_connect('draw_event', self.update_background)
-
+ self.cids=[]
+ self.canvas = None
+
self.rect = None
self.background = None
@@ -847,12 +844,30 @@
# Needed when dragging out of axes
self.buttonDown = False
self.prev = (0, 0)
+
+ self.new_axes(ax)
+
+ def new_axes(self,ax):
+ self.ax = ax
+ if self.canvas is not ax.figure.canvas:
+ for i,cid in enumerate(self.cids[:]):
+ try:
+ self.canvas.mpl_disconnect(cid)
+ except: #if the old canvas is dead or anything else unexpected
+ pass
+ self.cids.remove(cid)
+
+ self.canvas = ax.figure.canvas
+ self.cids.append(self.canvas.mpl_connect('motion_notify_event', self.onmove))
+ self.cids.append(self.canvas.mpl_connect('button_press_event', self.press))
+ self.cids.append(self.canvas.mpl_connect('button_release_event',
self.release))
+ self.cids.append(self.canvas.mpl_connect('draw_event',
self.update_background))
if self.direction == 'horizontal':
- trans = blended_transform_factory(self.ax.transData,
self.ax.transAxes)
+ trans = blend_xy_sep_transform(self.ax.transData,
self.ax.transAxes)
w,h = 0,1
else:
- trans = blended_transform_factory(self.ax.transAxes,
self.ax.transData)
+ trans = blend_xy_sep_transform(self.ax.transAxes,
self.ax.transData)
w,h = 1,0
self.rect = Rectangle( (0,0), w, h,
transform=trans,