@@ -27,20 +27,19 @@ def onselect(epress, erelease):
27
27
ax ._got_onselect = True
28
28
assert epress .xdata == 100
29
29
assert epress .ydata == 100
30
- assert erelease .xdata == 199
31
- assert erelease .ydata == 199
30
+ assert erelease .xdata == 200
31
+ assert erelease .ydata == 200
32
32
33
33
tool = widgets .RectangleSelector (ax , onselect , ** kwargs )
34
34
do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
35
- do_event (tool , 'onmove' , xdata = 199 , ydata = 199 , button = 1 )
36
-
35
+ do_event (tool , 'onmove' , xdata = 250 , ydata = 250 , button = 1 )
37
36
# purposely drag outside of axis for release
38
37
do_event (tool , 'release' , xdata = 250 , ydata = 250 , button = 1 )
39
38
40
39
if kwargs .get ('drawtype' , None ) not in ['line' , 'none' ]:
41
40
assert_allclose (tool .geometry ,
42
- [[100. , 100 , 199 , 199 , 100 ],
43
- [100 , 199 , 199 , 100 , 100 ]],
41
+ [[100. , 100 , 200 , 200 , 100 ],
42
+ [100 , 200 , 200 , 100 , 100 ]],
44
43
err_msg = tool .geometry )
45
44
46
45
assert ax ._got_onselect
@@ -259,8 +258,8 @@ def onselect(epress, erelease):
259
258
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
260
259
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
261
260
key = use_key )
262
- assert tool .extents == (xdata_new , extents [1 ] - xdiff ,
263
- ydata_new , extents [3 ] - ydiff )
261
+ assert_allclose ( tool .extents , (xdata_new , extents [1 ] - xdiff ,
262
+ ydata_new , extents [3 ] - ydiff ) )
264
263
265
264
266
265
@pytest .mark .parametrize ('add_state' , [True , False ])
@@ -273,7 +272,7 @@ def onselect(epress, erelease):
273
272
tool = widgets .RectangleSelector (ax , onselect , interactive = True )
274
273
# Create rectangle
275
274
click_and_drag (tool , start = (70 , 65 ), end = (120 , 115 ))
276
- assert tool .extents == (70.0 , 120.0 , 65.0 , 115.0 )
275
+ assert_allclose ( tool .extents , (70.0 , 120.0 , 65.0 , 115.0 ) )
277
276
278
277
if add_state :
279
278
tool .add_state ('square' )
@@ -288,8 +287,8 @@ def onselect(epress, erelease):
288
287
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
289
288
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
290
289
key = use_key )
291
- assert tool .extents == (extents [0 ], xdata_new ,
292
- extents [2 ], extents [3 ] + xdiff )
290
+ assert_allclose ( tool .extents , (extents [0 ], xdata_new ,
291
+ extents [2 ], extents [3 ] + xdiff ) )
293
292
294
293
# resize E handle
295
294
extents = tool .extents
@@ -344,6 +343,7 @@ def onselect(epress, erelease):
344
343
345
344
def test_rectangle_resize_square_center ():
346
345
ax = get_ax ()
346
+ ax .set_aspect (1 )
347
347
348
348
def onselect (epress , erelease ):
349
349
pass
@@ -414,6 +414,7 @@ def onselect(epress, erelease):
414
414
[widgets .RectangleSelector , widgets .EllipseSelector ])
415
415
def test_rectangle_rotate (selector_class ):
416
416
ax = get_ax ()
417
+ ax .set_aspect (1 )
417
418
418
419
def onselect (epress , erelease ):
419
420
pass
@@ -431,19 +432,19 @@ def onselect(epress, erelease):
431
432
click_and_drag (tool , start = (130 , 140 ), end = (120 , 145 ))
432
433
do_event (tool , 'on_key_press' , key = 'r' )
433
434
assert len (tool ._state ) == 0
434
- # Extents shouldn't change ( as shape of rectangle hasn't changed)
435
- assert tool .extents == ( 100 , 130 , 100 , 140 )
435
+ # Extents change as the selector remains rigid in display coordinates
436
+ assert_allclose ( tool .extents , ( 110.10 , 119.90 , 95.49 , 144.51 ), atol = 0.01 )
436
437
assert_allclose (tool .rotation , 25.56 , atol = 0.01 )
437
438
tool .rotation = 45
438
439
assert tool .rotation == 45
439
440
# Corners should move
440
441
assert_allclose (tool .corners ,
441
- np .array ([[118.53 , 139.75 , 111.46 , 90.25 ],
442
- [95.25 , 116.46 , 144.75 , 123.54 ]]), atol = 0.01 )
442
+ np .array ([[110.10 , 131.31 , 103.03 , 81.81 ],
443
+ [95.49 , 116.70 , 144.98 , 123.77 ]]), atol = 0.01 )
443
444
444
445
# Scale using top-right corner
445
446
click_and_drag (tool , start = (110 , 145 ), end = (110 , 160 ))
446
- assert_allclose (tool .extents , (100 , 139.75 , 100 , 151.82 ), atol = 0.01 )
447
+ assert_allclose (tool .extents , (110 , 110 , 145 , 160 ), atol = 0.01 )
447
448
448
449
if selector_class == widgets .RectangleSelector :
449
450
with pytest .raises (ValueError ):
@@ -471,39 +472,40 @@ def onselect(epress, erelease):
471
472
@pytest .mark .parametrize ('use_data_coordinates' , [False , True ])
472
473
def test_rectangle_resize_square_center_aspect (use_data_coordinates ):
473
474
ax = get_ax ()
474
- ax .set_aspect (0.8 )
475
+ ax .set_aspect (0.5 )
476
+ # Need to call a draw to update ax.transData
477
+ plt .gcf ().canvas .draw ()
475
478
476
479
def onselect (epress , erelease ):
477
480
pass
478
481
479
482
tool = widgets .RectangleSelector (ax , onselect , interactive = True ,
480
483
use_data_coordinates = use_data_coordinates )
481
- # Create rectangle
482
- click_and_drag (tool , start = (70 , 65 ), end = (120 , 115 ))
483
- assert_allclose (tool .extents , (70.0 , 120.0 , 65.0 , 115.0 ))
484
484
tool .add_state ('square' )
485
485
tool .add_state ('center' )
486
+ # Create rectangle, width 50 in data coordinates
487
+ click_and_drag (tool , start = (70 , 65 ), end = (120 , 65 ))
486
488
487
489
if use_data_coordinates :
488
- # resize E handle
489
- extents = tool .extents
490
- xdata , ydata , width = extents [1 ], extents [3 ], extents [1 ] - extents [0 ]
491
- xdiff , ycenter = 10 , extents [2 ] + (extents [3 ] - extents [2 ]) / 2
492
- xdata_new , ydata_new = xdata + xdiff , ydata
493
- ychange = width / 2 + xdiff
494
- click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
495
- assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
496
- ycenter - ychange , ycenter + ychange ])
490
+ assert_allclose (tool .extents , (20 , 120 , 15 , 115 ))
497
491
else :
498
- # resize E handle
499
- extents = tool .extents
500
- xdata , ydata = extents [1 ], extents [3 ]
501
- xdiff = 10
502
- xdata_new , ydata_new = xdata + xdiff , ydata
503
- ychange = xdiff * 1 / tool ._aspect_ratio_correction
504
- click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
505
- assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
506
- 46.25 , 133.75 ])
492
+ assert_allclose (tool .extents , (20 , 120 , - 35 , 165 ))
493
+
494
+ # resize E handle
495
+ extents = tool .extents
496
+ xdata , ydata = extents [1 ], extents [3 ]
497
+ xdiff = 10
498
+ xdata_new , ydata_new = xdata + xdiff , ydata
499
+ if use_data_coordinates :
500
+ # In data coordinates the difference should be equal in both directions
501
+ ydiff = xdiff
502
+ else :
503
+ # In display coordiantes, the change in data coordinates should be
504
+ # different in each direction
505
+ ydiff = xdiff / tool .ax ._get_aspect_ratio ()
506
+ click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
507
+ assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
508
+ extents [2 ] - ydiff , extents [3 ] + ydiff ])
507
509
508
510
509
511
def test_ellipse ():
0 commit comments