Hello list,
refering to the thread 'activate/deactivate RectangleSelector' of the
developer-mailing-list I thought a bit about the problem because I found it
quite irritating that the RS couldn't be stopped during zooming.
I attached a patch including Martin's example and my proposal to (de)activate
RectangleSelector including deactivation during zoom mode.
I hope this is helpful for others too and can be part of matplotlib.
best regards,
Matthias
PS: Should I send those mails to developer mailing list?
Index: widgets.py
===================================================================
--- widgets.py (revision 3261)
+++ widgets.py (working copy)
@@ -12,6 +12,7 @@
from lines import Line2D
from numerix import array
from transforms import blend_xy_sep_transform
+from matplotlib._pylab_helpers import Gcf
class LockDraw:
"""
@@ -952,24 +953,40 @@
warnings.warn('Use SpanSelector instead!', DeprecationWarning)
SpanSelector.__init__(self, ax, onselect, 'horizontal', **kwargs)
+
class RectangleSelector:
"""
Select a min/max range of the x axes for a matplotlib Axes
Example usage:
- ax = subplot(111)
- ax.plot(x,y)
+ from matplotlib.widgets import RectangleSelector
+ from pylab import *
- def onselect(eclick, erelease):
+ def onselect(eclick, erelease):
'eclick and erelease are matplotlib events at press and release'
- print 'startposition : (%f,%f)'%(eclick.xdata, eclick.ydata)
- print 'endposition : (%f,%f)'%(erelease.xdata, erelease.ydata)
- print 'used button : ', eclick.button
+ print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
+ print ' endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)
+ print ' used button : ', eclick.button
- span = Selector(ax, onselect,drawtype='box')
- show()
+ def toggle_Selector(event):
+ print ' Key pressed.'
+ if event.key in ['Q', 'q'] and toggle_Selector.RS.active:
+ print ' RectangleSelector deactivated.'
+ toggle_Selector.RS.set_active(False)
+ if event.key in ['A', 'a'] and not toggle_Selector.RS.active:
+ print ' RectangleSelector activated.'
+ toggle_Selector.RS.set_active(True)
+ x = arange(100)/(99.0)
+ y = sin(x)
+ fig = figure
+ ax = subplot(111)
+ ax.plot(x,y)
+
+ toggle_Selector.RS = RectangleSelector(ax, onselect, drawtype='line')
+ connect('key_press_event', toggle_Selector)
+ show()
"""
def __init__(self, ax, onselect, drawtype='box',
minspanx=None, minspany=None, useblit=False,
@@ -998,8 +1015,6 @@
Use type if you want the mouse to draw a line, a box or nothing
between click and actual position ny setting
drawtype = 'line', drawtype='box' or drawtype = 'none'.
-
-
"""
self.ax = ax
self.visible = True
@@ -1009,6 +1024,7 @@
self.canvas.mpl_connect('button_release_event', self.release)
self.canvas.mpl_connect('draw_event', self.update_background)
+ self.active = True # for activation / deactivation
self.to_draw = None
self.background = None
@@ -1049,6 +1065,14 @@
def ignore(self, event):
'return True if event should be ignored'
+ # If RectangleSelector is not active :
+ if not self.active:
+ return True
+
+ # If zoom mode was activated
+ if not (Gcf.get_active().toolbar.mode == ''):
+ return True
+
# If no button was pressed yet ignore the event if it was out
# of the axes
if self.eventpress == None:
@@ -1139,6 +1163,17 @@
self.update()
return False
+ def set_active(self, active):
+ """ Use this to activate / deactivate the RectangleSelector
+
+ from your program with an boolean variable 'active'.
+ """
+ self.active = active
+
+ def get_active(self):
+ """ to get status of active mode (boolean variable)"""
+ return self.active
+
class Lasso(Widget):
def __init__(self, ax, xy, callback=None, useblit=True):
self.axes = ax
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users