11
11
import matplotlib .pyplot as plt
12
12
from matplotlib .testing .decorators import cleanup
13
13
14
+ from numpy .testing import assert_allclose
14
15
15
- def get_event (ax , button = 1 , xdata = 0 , ydata = 0 , key = None , step = 1 ):
16
+
17
+ def get_ax ():
18
+ fig , ax = plt .subplots (1 , 1 )
19
+ ax .plot ([0 , 200 ], [0 , 200 ])
20
+ ax .set_aspect (1.0 )
21
+ ax .figure .canvas .draw ()
22
+ return ax
23
+
24
+
25
+ def do_event (tool , etype , button = 1 , xdata = 0 , ydata = 0 , key = None , step = 1 ):
16
26
"""
17
27
*name*
18
28
the event name
@@ -51,6 +61,7 @@ def get_event(ax, button=1, xdata=0, ydata=0, key=None, step=1):
51
61
"""
52
62
event = mock .Mock ()
53
63
event .button = button
64
+ ax = tool .ax
54
65
event .x , event .y = ax .transData .transform ([(xdata , ydata ),
55
66
(xdata , ydata )])[00 ]
56
67
event .xdata , event .ydata = xdata , ydata
@@ -60,32 +71,33 @@ def get_event(ax, button=1, xdata=0, ydata=0, key=None, step=1):
60
71
event .step = step
61
72
event .guiEvent = None
62
73
event .name = 'Custom'
63
- return event
74
+
75
+ func = getattr (tool , etype )
76
+ func (event )
64
77
65
78
66
79
@cleanup
67
80
def check_rectangle (** kwargs ):
68
- fig , ax = plt .subplots (1 , 1 )
69
- ax .plot ([0 , 200 ], [0 , 200 ])
70
- ax .figure .canvas .draw ()
81
+ ax = get_ax ()
71
82
72
83
def onselect (epress , erelease ):
73
84
ax ._got_onselect = True
74
85
assert epress .xdata == 100
75
86
assert epress .ydata == 100
76
- assert erelease .xdata == 200
77
- assert erelease .ydata == 200
87
+ assert erelease .xdata == 199
88
+ assert erelease .ydata == 199
78
89
79
90
tool = widgets .RectangleSelector (ax , onselect , ** kwargs )
80
- event = get_event (ax , xdata = 100 , ydata = 100 , button = 1 )
81
- tool .press (event )
82
-
83
- event = get_event (ax , xdata = 125 , ydata = 125 , button = 1 )
84
- tool .onmove (event )
91
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
92
+ do_event (tool , 'onmove' , xdata = 199 , ydata = 199 , button = 1 )
85
93
86
94
# purposely drag outside of axis for release
87
- event = get_event (ax , xdata = 250 , ydata = 250 , button = 1 )
88
- tool .release (event )
95
+ do_event (tool , 'release' , xdata = 250 , ydata = 250 , button = 1 )
96
+
97
+ if kwargs .get ('drawtype' , None ) not in ['line' , 'none' ]:
98
+ assert_allclose (tool .geometry ,
99
+ [[100. , 100 , 199 , 199 , 100 ], [100 , 199 , 199 , 100 , 100 ]],
100
+ err_msg = tool .geometry )
89
101
90
102
assert ax ._got_onselect
91
103
@@ -99,11 +111,101 @@ def test_rectangle_selector():
99
111
check_rectangle (rectprops = dict (fill = True ))
100
112
101
113
114
+ @cleanup
115
+ def test_ellipse ():
116
+ """For ellipse, test out the key modifiers"""
117
+ ax = get_ax ()
118
+
119
+ def onselect (epress , erelease ):
120
+ pass
121
+
122
+ tool = widgets .EllipseSelector (ax , onselect = onselect ,
123
+ maxdist = 10 , interactive = True )
124
+ tool .extents = (100 , 150 , 100 , 150 )
125
+
126
+ # drag the rectangle
127
+ do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 ,
128
+ key = ' ' )
129
+ do_event (tool , 'onmove' , xdata = 30 , ydata = 30 , button = 1 )
130
+ do_event (tool , 'release' , xdata = 30 , ydata = 30 , button = 1 )
131
+ assert tool .extents == (120 , 170 , 120 , 170 ), tool .extents
132
+
133
+ # create from center
134
+ do_event (tool , 'on_key_press' , xdata = 100 , ydata = 100 , button = 1 ,
135
+ key = 'control' )
136
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
137
+ do_event (tool , 'onmove' , xdata = 125 , ydata = 125 , button = 1 )
138
+ do_event (tool , 'release' , xdata = 125 , ydata = 125 , button = 1 )
139
+ do_event (tool , 'on_key_release' , xdata = 100 , ydata = 100 , button = 1 ,
140
+ key = 'control' )
141
+ assert tool .extents == (75 , 125 , 75 , 125 ), tool .extents
142
+
143
+ # create a square
144
+ do_event (tool , 'on_key_press' , xdata = 10 , ydata = 10 , button = 1 ,
145
+ key = 'shift' )
146
+ do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
147
+ do_event (tool , 'onmove' , xdata = 35 , ydata = 30 , button = 1 )
148
+ do_event (tool , 'release' , xdata = 35 , ydata = 30 , button = 1 )
149
+ do_event (tool , 'on_key_release' , xdata = 10 , ydata = 10 , button = 1 ,
150
+ key = 'shift' )
151
+ extents = [int (e ) for e in tool .extents ]
152
+ assert extents == [10 , 35 , 10 , 34 ]
153
+
154
+ # create a square from center
155
+ do_event (tool , 'on_key_press' , xdata = 100 , ydata = 100 , button = 1 ,
156
+ key = 'ctrl+shift' )
157
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
158
+ do_event (tool , 'onmove' , xdata = 125 , ydata = 130 , button = 1 )
159
+ do_event (tool , 'release' , xdata = 125 , ydata = 130 , button = 1 )
160
+ do_event (tool , 'on_key_release' , xdata = 100 , ydata = 100 , button = 1 ,
161
+ key = 'ctrl+shift' )
162
+ extents = [int (e ) for e in tool .extents ]
163
+ assert extents == [70 , 129 , 70 , 130 ], extents
164
+
165
+ assert tool .geometry .shape == (2 , 74 )
166
+ assert_allclose (tool .geometry [:, 0 ], [70. , 100 ])
167
+
168
+
169
+ @cleanup
170
+ def test_rectangle_handles ():
171
+ ax = get_ax ()
172
+
173
+ def onselect (epress , erelease ):
174
+ pass
175
+
176
+ tool = widgets .RectangleSelector (ax , onselect = onselect ,
177
+ maxdist = 10 , interactive = True )
178
+ tool .extents = (100 , 150 , 100 , 150 )
179
+
180
+ assert tool .corners == (
181
+ (100 , 150 , 150 , 100 ), (100 , 100 , 150 , 150 ))
182
+ assert tool .extents == (100 , 150 , 100 , 150 )
183
+ assert tool .edge_centers == (
184
+ (100 , 125.0 , 150 , 125.0 ), (125.0 , 100 , 125.0 , 150 ))
185
+ assert tool .extents == (100 , 150 , 100 , 150 )
186
+
187
+ # grab a corner and move it
188
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 )
189
+ do_event (tool , 'onmove' , xdata = 120 , ydata = 120 )
190
+ do_event (tool , 'release' , xdata = 120 , ydata = 120 )
191
+ assert tool .extents == (120 , 150 , 120 , 150 )
192
+
193
+ # grab the center and move it
194
+ do_event (tool , 'press' , xdata = 132 , ydata = 132 )
195
+ do_event (tool , 'onmove' , xdata = 120 , ydata = 120 )
196
+ do_event (tool , 'release' , xdata = 120 , ydata = 120 )
197
+ assert tool .extents == (108 , 138 , 108 , 138 )
198
+
199
+ # create a new rectangle
200
+ do_event (tool , 'press' , xdata = 10 , ydata = 10 )
201
+ do_event (tool , 'onmove' , xdata = 100 , ydata = 100 )
202
+ do_event (tool , 'release' , xdata = 100 , ydata = 100 )
203
+ assert tool .extents == (10 , 100 , 10 , 100 )
204
+
205
+
102
206
@cleanup
103
207
def check_span (* args , ** kwargs ):
104
- fig , ax = plt .subplots (1 , 1 )
105
- ax .plot ([0 , 200 ], [0 , 200 ])
106
- ax .figure .canvas .draw ()
208
+ ax = get_ax ()
107
209
108
210
def onselect (vmin , vmax ):
109
211
ax ._got_onselect = True
@@ -119,14 +221,9 @@ def onmove(vmin, vmax):
119
221
kwargs ['onmove_callback' ] = onmove
120
222
121
223
tool = widgets .SpanSelector (ax , onselect , * args , ** kwargs )
122
- event = get_event (ax , xdata = 100 , ydata = 100 , button = 1 )
123
- tool .press (event )
124
-
125
- event = get_event (ax , xdata = 125 , ydata = 125 , button = 1 )
126
- tool .onmove (event )
127
-
128
- event = get_event (ax , xdata = 150 , ydata = 150 , button = 1 )
129
- tool .release (event )
224
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
225
+ do_event (tool , 'onmove' , xdata = 125 , ydata = 125 , button = 1 )
226
+ do_event (tool , 'release' , xdata = 150 , ydata = 150 , button = 1 )
130
227
131
228
assert ax ._got_onselect
132
229
@@ -142,24 +239,16 @@ def test_span_selector():
142
239
143
240
@cleanup
144
241
def check_lasso_selector (** kwargs ):
145
- fig , ax = plt .subplots (1 , 1 )
146
- ax = plt .gca ()
147
- ax .plot ([0 , 200 ], [0 , 200 ])
148
- ax .figure .canvas .draw ()
242
+ ax = get_ax ()
149
243
150
244
def onselect (verts ):
151
245
ax ._got_onselect = True
152
246
assert verts == [(100 , 100 ), (125 , 125 ), (150 , 150 )]
153
247
154
248
tool = widgets .LassoSelector (ax , onselect , ** kwargs )
155
- event = get_event (ax , xdata = 100 , ydata = 100 , button = 1 )
156
- tool .press (event )
157
-
158
- event = get_event (ax , xdata = 125 , ydata = 125 , button = 1 )
159
- tool .onmove (event )
160
-
161
- event = get_event (ax , xdata = 150 , ydata = 150 , button = 1 )
162
- tool .release (event )
249
+ do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
250
+ do_event (tool , 'onmove' , xdata = 125 , ydata = 125 , button = 1 )
251
+ do_event (tool , 'release' , xdata = 150 , ydata = 150 , button = 1 )
163
252
164
253
assert ax ._got_onselect
165
254
0 commit comments