@@ -32,20 +32,19 @@ def onselect(epress, erelease):
32
32
ax ._got_onselect = True
33
33
assert epress .xdata == 100
34
34
assert epress .ydata == 100
35
- assert erelease .xdata == 199
36
- assert erelease .ydata == 199
35
+ assert erelease .xdata == 200
36
+ assert erelease .ydata == 200
37
37
38
38
tool = widgets .RectangleSelector (ax , onselect , ** kwargs )
39
39
do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
40
- do_event (tool , 'onmove' , xdata = 199 , ydata = 199 , button = 1 )
41
-
40
+ do_event (tool , 'onmove' , xdata = 250 , ydata = 250 , button = 1 )
42
41
# purposely drag outside of axis for release
43
42
do_event (tool , 'release' , xdata = 250 , ydata = 250 , button = 1 )
44
43
45
44
if kwargs .get ('drawtype' , None ) not in ['line' , 'none' ]:
46
45
assert_allclose (tool .geometry ,
47
- [[100. , 100 , 199 , 199 , 100 ],
48
- [100 , 199 , 199 , 100 , 100 ]],
46
+ [[100. , 100 , 200 , 200 , 100 ],
47
+ [100 , 200 , 200 , 100 , 100 ]],
49
48
err_msg = tool .geometry )
50
49
51
50
assert ax ._got_onselect
@@ -280,16 +279,16 @@ def test_rectangle_resize_center(ax, add_state):
280
279
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
281
280
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
282
281
key = use_key )
283
- assert tool .extents == (xdata_new , extents [1 ] - xdiff ,
284
- ydata_new , extents [3 ] - ydiff )
282
+ assert_allclose ( tool .extents , (xdata_new , extents [1 ] - xdiff ,
283
+ ydata_new , extents [3 ] - ydiff ) )
285
284
286
285
287
286
@pytest .mark .parametrize ('add_state' , [True , False ])
288
287
def test_rectangle_resize_square (ax , add_state ):
289
288
tool = widgets .RectangleSelector (ax , onselect = noop , interactive = True )
290
289
# Create rectangle
291
290
click_and_drag (tool , start = (70 , 65 ), end = (120 , 115 ))
292
- assert tool .extents == (70.0 , 120.0 , 65.0 , 115.0 )
291
+ assert_allclose ( tool .extents , (70.0 , 120.0 , 65.0 , 115.0 ) )
293
292
294
293
if add_state :
295
294
tool .add_state ('square' )
@@ -304,8 +303,8 @@ def test_rectangle_resize_square(ax, add_state):
304
303
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
305
304
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
306
305
key = use_key )
307
- assert tool .extents == (extents [0 ], xdata_new ,
308
- extents [2 ], extents [3 ] + xdiff )
306
+ assert_allclose ( tool .extents , (extents [0 ], xdata_new ,
307
+ extents [2 ], extents [3 ] + xdiff ) )
309
308
310
309
# resize E handle
311
310
extents = tool .extents
@@ -354,11 +353,12 @@ def test_rectangle_resize_square(ax, add_state):
354
353
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
355
354
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
356
355
key = use_key )
357
- assert_allclose (tool .extents , (extents [ 0 ] + ydiff , extents [1 ],
358
- ydata_new , extents [3 ]))
356
+ assert_allclose (tool .extents , (xdata_new , extents [1 ],
357
+ extents [ 2 ] + xdiff , extents [3 ]))
359
358
360
359
361
360
def test_rectangle_resize_square_center (ax ):
361
+ ax .set_aspect (1 )
362
362
tool = widgets .RectangleSelector (ax , onselect = noop , interactive = True )
363
363
# Create rectangle
364
364
click_and_drag (tool , start = (70 , 65 ), end = (120 , 115 ))
@@ -424,6 +424,7 @@ def test_rectangle_resize_square_center(ax):
424
424
@pytest .mark .parametrize ('selector_class' ,
425
425
[widgets .RectangleSelector , widgets .EllipseSelector ])
426
426
def test_rectangle_rotate (ax , selector_class ):
427
+ ax .set_aspect (1 )
427
428
tool = selector_class (ax , onselect = noop , interactive = True )
428
429
# Draw rectangle
429
430
click_and_drag (tool , start = (100 , 100 ), end = (130 , 140 ))
@@ -437,19 +438,19 @@ def test_rectangle_rotate(ax, selector_class):
437
438
click_and_drag (tool , start = (130 , 140 ), end = (120 , 145 ))
438
439
do_event (tool , 'on_key_press' , key = 'r' )
439
440
assert len (tool ._state ) == 0
440
- # Extents shouldn't change ( as shape of rectangle hasn't changed)
441
- assert tool .extents == ( 100 , 130 , 100 , 140 )
441
+ # Extents change as the selector remains rigid in display coordinates
442
+ assert_allclose ( tool .extents , ( 110.10 , 119.90 , 95.49 , 144.51 ), atol = 0.01 )
442
443
assert_allclose (tool .rotation , 25.56 , atol = 0.01 )
443
444
tool .rotation = 45
444
445
assert tool .rotation == 45
445
446
# Corners should move
446
447
assert_allclose (tool .corners ,
447
- np .array ([[118.53 , 139.75 , 111.46 , 90.25 ],
448
- [95.25 , 116.46 , 144.75 , 123.54 ]]), atol = 0.01 )
448
+ np .array ([[110.10 , 131.31 , 103.03 , 81.81 ],
449
+ [95.49 , 116.70 , 144.98 , 123.77 ]]), atol = 0.01 )
449
450
450
451
# Scale using top-right corner
451
452
click_and_drag (tool , start = (110 , 145 ), end = (110 , 160 ))
452
- assert_allclose (tool .extents , (100 , 139.75 , 100 , 151.82 ), atol = 0.01 )
453
+ assert_allclose (tool .extents , (110 , 110 , 145 , 160 ), atol = 0.01 )
453
454
454
455
if selector_class == widgets .RectangleSelector :
455
456
with pytest .raises (ValueError ):
@@ -471,36 +472,38 @@ def test_rectange_add_remove_set(ax):
471
472
472
473
@pytest .mark .parametrize ('use_data_coordinates' , [False , True ])
473
474
def test_rectangle_resize_square_center_aspect (ax , use_data_coordinates ):
474
- ax .set_aspect (0.8 )
475
+ ax = get_ax ()
476
+ ax .set_aspect (0.5 )
477
+ # Need to call a draw to update ax.transData
478
+ plt .gcf ().canvas .draw ()
475
479
476
480
tool = widgets .RectangleSelector (ax , onselect = noop , interactive = True ,
477
481
use_data_coordinates = use_data_coordinates )
478
- # Create rectangle
479
- click_and_drag (tool , start = (70 , 65 ), end = (120 , 115 ))
480
- assert_allclose (tool .extents , (70.0 , 120.0 , 65.0 , 115.0 ))
481
482
tool .add_state ('square' )
482
483
tool .add_state ('center' )
484
+ # Create rectangle, width 50 in data coordinates
485
+ click_and_drag (tool , start = (70 , 65 ), end = (120 , 65 ))
483
486
484
487
if use_data_coordinates :
485
- # resize E handle
486
- extents = tool .extents
487
- xdata , ydata , width = extents [1 ], extents [3 ], extents [1 ] - extents [0 ]
488
- xdiff , ycenter = 10 , extents [2 ] + (extents [3 ] - extents [2 ]) / 2
489
- xdata_new , ydata_new = xdata + xdiff , ydata
490
- ychange = width / 2 + xdiff
491
- click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
492
- assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
493
- ycenter - ychange , ycenter + ychange ])
488
+ assert_allclose (tool .extents , (20 , 120 , 15 , 115 ))
494
489
else :
495
- # resize E handle
496
- extents = tool .extents
497
- xdata , ydata = extents [1 ], extents [3 ]
498
- xdiff = 10
499
- xdata_new , ydata_new = xdata + xdiff , ydata
500
- ychange = xdiff * 1 / tool ._aspect_ratio_correction
501
- click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
502
- assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
503
- 46.25 , 133.75 ])
490
+ assert_allclose (tool .extents , (20 , 120 , - 35 , 165 ))
491
+
492
+ # resize E handle
493
+ extents = tool .extents
494
+ xdata , ydata = extents [1 ], extents [3 ]
495
+ xdiff = 10
496
+ xdata_new , ydata_new = xdata + xdiff , ydata
497
+ if use_data_coordinates :
498
+ # In data coordinates the difference should be equal in both directions
499
+ ydiff = xdiff
500
+ else :
501
+ # In display coordiantes, the change in data coordinates should be
502
+ # different in each direction
503
+ ydiff = xdiff / tool .ax ._get_aspect_ratio ()
504
+ click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
505
+ assert_allclose (tool .extents , [extents [0 ] - xdiff , xdata_new ,
506
+ extents [2 ] - ydiff , extents [3 ] + ydiff ])
504
507
505
508
506
509
def test_ellipse (ax ):
@@ -1409,6 +1412,14 @@ def test_polygon_selector_box(ax):
1409
1412
polygon_place_vertex (* verts [3 ]) +
1410
1413
polygon_place_vertex (* verts [0 ]))
1411
1414
1415
+ # Set smaller axes limits to reduce errors in converting from data to
1416
+ # display coords. The canvas size is 640 x 640, so we need a tolerance of
1417
+ # (data width / canvas width) = 50 / 640 ~ 0.08 when comparing points in
1418
+ # data space
1419
+ ax .set_xlim (0 , 50 )
1420
+ ax .set_ylim (0 , 50 )
1421
+ atol = 0.08
1422
+
1412
1423
# Create selector
1413
1424
tool = widgets .PolygonSelector (ax , onselect = noop , draw_bounding_box = True )
1414
1425
for (etype , event_args ) in event_sequence :
@@ -1424,25 +1435,25 @@ def test_polygon_selector_box(ax):
1424
1435
canvas .motion_notify_event (* t .transform ((20 , 20 )))
1425
1436
canvas .button_release_event (* t .transform ((20 , 20 )), 1 )
1426
1437
np .testing .assert_allclose (
1427
- tool .verts , [(10 , 0 ), (0 , 10 ), (10 , 20 ), (20 , 10 )])
1438
+ tool .verts , [(10 , 0 ), (0 , 10 ), (10 , 20 ), (20 , 10 )], atol = atol )
1428
1439
1429
1440
# Move using the center of the bounding box
1430
1441
canvas .button_press_event (* t .transform ((10 , 10 )), 1 )
1431
1442
canvas .motion_notify_event (* t .transform ((30 , 30 )))
1432
1443
canvas .button_release_event (* t .transform ((30 , 30 )), 1 )
1433
1444
np .testing .assert_allclose (
1434
- tool .verts , [(30 , 20 ), (20 , 30 ), (30 , 40 ), (40 , 30 )])
1445
+ tool .verts , [(30 , 20 ), (20 , 30 ), (30 , 40 ), (40 , 30 )], atol = atol )
1435
1446
1436
1447
# Remove a point from the polygon and check that the box extents update
1437
1448
np .testing .assert_allclose (
1438
- tool ._box .extents , (20.0 , 40.0 , 20.0 , 40.0 ))
1449
+ tool ._box .extents , (20.0 , 40.0 , 20.0 , 40.0 ), atol = atol )
1439
1450
1440
1451
canvas .button_press_event (* t .transform ((30 , 20 )), 3 )
1441
1452
canvas .button_release_event (* t .transform ((30 , 20 )), 3 )
1442
1453
np .testing .assert_allclose (
1443
- tool .verts , [(20 , 30 ), (30 , 40 ), (40 , 30 )])
1454
+ tool .verts , [(20 , 30 ), (30 , 40 ), (40 , 30 )], atol = atol )
1444
1455
np .testing .assert_allclose (
1445
- tool ._box .extents , (20.0 , 40.0 , 30.0 , 40.0 ))
1456
+ tool ._box .extents , (20.0 , 40.0 , 30.0 , 40.0 ), atol = atol )
1446
1457
1447
1458
1448
1459
@pytest .mark .parametrize (
0 commit comments