@@ -979,7 +979,7 @@ class RectangleSelector:
979979 """
980980 Select a min/max range of the x axes for a matplotlib Axes
981981
982- Example usage:
982+ Example usage::
983983
984984 from matplotlib.widgets import RectangleSelector
985985 from pylab import *
@@ -1011,7 +1011,7 @@ def toggle_selector(event):
10111011 """
10121012 def __init__ (self , ax , onselect , drawtype = 'box' ,
10131013 minspanx = None , minspany = None , useblit = False ,
1014- lineprops = None , rectprops = None ):
1014+ lineprops = None , rectprops = None , spancoords = 'data' ):
10151015
10161016 """
10171017 Create a selector in ax. When a selection is made, clear
@@ -1035,7 +1035,12 @@ def __init__(self, ax, onselect, drawtype='box',
10351035
10361036 Use type if you want the mouse to draw a line, a box or nothing
10371037 between click and actual position ny setting
1038+
10381039 drawtype = 'line', drawtype='box' or drawtype = 'none'.
1040+
1041+ spancoords is one of 'data' or 'pixels'. If 'data', minspanx
1042+ and minspanx will be interpreted in the same coordinates as
1043+ the x and ya axis, if 'pixels', they are in pixels
10391044 """
10401045 self .ax = ax
10411046 self .visible = True
@@ -1072,6 +1077,10 @@ def __init__(self, ax, onselect, drawtype='box',
10721077 self .useblit = useblit
10731078 self .minspanx = minspanx
10741079 self .minspany = minspany
1080+
1081+ assert (spancoords in ('data' , 'pixels' ))
1082+
1083+ self .spancoords = spancoords
10751084 self .drawtype = drawtype
10761085 # will save the data (position at mouseclick)
10771086 self .eventpress = None
@@ -1125,14 +1134,21 @@ def release(self, event):
11251134 self .canvas .draw ()
11261135 # release coordinates, button, ...
11271136 self .eventrelease = event
1128- xmin , ymin = self .eventpress .xdata , self .eventpress .ydata
1129- xmax , ymax = self .eventrelease .xdata , self .eventrelease .ydata
1130- # calculate dimensions of box or line get values in the right
1131- # order
1132- if xmin > xmax : xmin , xmax = xmax , xmin
1133- if ymin > ymax : ymin , ymax = ymax , ymin
1137+
1138+ if self .spancoords == 'data' :
1139+ xmin , ymin = self .eventpress .xdata , self .eventpress .ydata
1140+ xmax , ymax = self .eventrelease .xdata , self .eventrelease .ydata
1141+ # calculate dimensions of box or line get values in the right
1142+ # order
1143+ elif self .spancoords == 'pixels' :
1144+ xmin , ymin = self .eventpress .x , self .eventpress .y
1145+ xmax , ymax = self .eventrelease .x , self .eventrelease .y
1146+ else :
1147+ raise ValueError ('spancoords must be "data" or "pixels"' )
11341148
11351149
1150+ if xmin > xmax : xmin , xmax = xmax , xmin
1151+ if ymin > ymax : ymin , ymax = ymax , ymin
11361152
11371153 spanx = xmax - xmin
11381154 spany = ymax - ymin
0 commit comments