3
3
import matplotlib .widgets as widgets
4
4
import matplotlib .pyplot as plt
5
5
from matplotlib .testing .decorators import check_figures_equal , image_comparison
6
- from matplotlib .testing .widgets import do_event , get_ax , mock_event
6
+ from matplotlib .testing .widgets import (click_and_move , do_event , get_ax ,
7
+ mock_event )
7
8
8
9
import numpy as np
9
10
from numpy .testing import assert_allclose
@@ -81,9 +82,7 @@ def onselect(epress, erelease):
81
82
tool = widgets .RectangleSelector (ax , onselect , interactive = True ,
82
83
drag_from_anywhere = drag_from_anywhere )
83
84
# Create rectangle
84
- do_event (tool , 'press' , xdata = 0 , ydata = 10 , button = 1 )
85
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
86
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
85
+ click_and_move (tool , start = [0 , 10 ], end = [100 , 120 ])
87
86
assert tool .center == (50 , 65 )
88
87
# Drag inside rectangle, but away from centre handle
89
88
#
@@ -92,15 +91,11 @@ def onselect(epress, erelease):
92
91
#
93
92
# If drag_from_anywhere == False, this will create a new rectangle with
94
93
# center (30, 20)
95
- do_event (tool , 'press' , xdata = 25 , ydata = 15 , button = 1 )
96
- do_event (tool , 'onmove' , xdata = 35 , ydata = 25 , button = 1 )
97
- do_event (tool , 'release' , xdata = 35 , ydata = 25 , button = 1 )
94
+ click_and_move (tool , start = [25 , 15 ], end = [35 , 25 ])
98
95
assert tool .center == new_center
99
96
# Check that in both cases, dragging outside the rectangle draws a new
100
97
# rectangle
101
- do_event (tool , 'press' , xdata = 175 , ydata = 185 , button = 1 )
102
- do_event (tool , 'onmove' , xdata = 185 , ydata = 195 , button = 1 )
103
- do_event (tool , 'release' , xdata = 185 , ydata = 195 , button = 1 )
98
+ click_and_move (tool , start = [175 , 185 ], end = [185 , 195 ])
104
99
assert tool .center == (180 , 190 )
105
100
106
101
@@ -114,9 +109,7 @@ def onselect(epress, erelease):
114
109
props = dict (facecolor = 'b' , alpha = 0.2 ),
115
110
handle_props = dict (alpha = 0.5 ))
116
111
# Create rectangle
117
- do_event (tool , 'press' , xdata = 0 , ydata = 10 , button = 1 )
118
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
119
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
112
+ click_and_move (tool , start = [0 , 10 ], end = [100 , 120 ])
120
113
121
114
artist = tool ._selection_artist
122
115
assert artist .get_facecolor () == mcolors .to_rgba ('b' , alpha = 0.2 )
@@ -522,42 +515,20 @@ def onselect(epress, erelease):
522
515
tool .extents = (100 , 150 , 100 , 150 )
523
516
524
517
# drag the rectangle
525
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 ,
526
- key = ' ' )
527
-
528
- do_event (tool , 'onmove' , xdata = 30 , ydata = 30 , button = 1 )
529
- do_event (tool , 'release' , xdata = 30 , ydata = 30 , button = 1 )
518
+ click_and_move (tool , start = [125 , 125 ], end = [145 , 145 ])
530
519
assert tool .extents == (120 , 170 , 120 , 170 )
531
520
532
521
# create from center
533
- do_event (tool , 'on_key_press' , xdata = 100 , ydata = 100 , button = 1 ,
534
- key = 'control' )
535
- do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
536
- do_event (tool , 'onmove' , xdata = 125 , ydata = 125 , button = 1 )
537
- do_event (tool , 'release' , xdata = 125 , ydata = 125 , button = 1 )
538
- do_event (tool , 'on_key_release' , xdata = 100 , ydata = 100 , button = 1 ,
539
- key = 'control' )
522
+ click_and_move (tool , start = [100 , 100 ], end = [125 , 125 ], key = 'control' )
540
523
assert tool .extents == (75 , 125 , 75 , 125 )
541
524
542
525
# create a square
543
- do_event (tool , 'on_key_press' , xdata = 10 , ydata = 10 , button = 1 ,
544
- key = 'shift' )
545
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
546
- do_event (tool , 'onmove' , xdata = 35 , ydata = 30 , button = 1 )
547
- do_event (tool , 'release' , xdata = 35 , ydata = 30 , button = 1 )
548
- do_event (tool , 'on_key_release' , xdata = 10 , ydata = 10 , button = 1 ,
549
- key = 'shift' )
526
+ click_and_move (tool , start = [10 , 10 ], end = [35 , 30 ], key = 'shift' )
550
527
extents = [int (e ) for e in tool .extents ]
551
528
assert extents == [10 , 35 , 10 , 35 ]
552
529
553
530
# create a square from center
554
- do_event (tool , 'on_key_press' , xdata = 100 , ydata = 100 , button = 1 ,
555
- key = 'ctrl+shift' )
556
- do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
557
- do_event (tool , 'onmove' , xdata = 125 , ydata = 130 , button = 1 )
558
- do_event (tool , 'release' , xdata = 125 , ydata = 130 , button = 1 )
559
- do_event (tool , 'on_key_release' , xdata = 100 , ydata = 100 , button = 1 ,
560
- key = 'ctrl+shift' )
531
+ click_and_move (tool , start = [100 , 100 ], end = [125 , 130 ], key = 'ctrl+shift' )
561
532
extents = [int (e ) for e in tool .extents ]
562
533
assert extents == [70 , 130 , 70 , 130 ]
563
534
@@ -585,21 +556,15 @@ def onselect(epress, erelease):
585
556
assert tool .extents == (100 , 150 , 100 , 150 )
586
557
587
558
# grab a corner and move it
588
- do_event (tool , 'press' , xdata = 100 , ydata = 100 )
589
- do_event (tool , 'onmove' , xdata = 120 , ydata = 120 )
590
- do_event (tool , 'release' , xdata = 120 , ydata = 120 )
559
+ click_and_move (tool , start = [100 , 100 ], end = [120 , 120 ])
591
560
assert tool .extents == (120 , 150 , 120 , 150 )
592
561
593
562
# grab the center and move it
594
- do_event (tool , 'press' , xdata = 132 , ydata = 132 )
595
- do_event (tool , 'onmove' , xdata = 120 , ydata = 120 )
596
- do_event (tool , 'release' , xdata = 120 , ydata = 120 )
563
+ click_and_move (tool , start = [132 , 132 ], end = [120 , 120 ])
597
564
assert tool .extents == (108 , 138 , 108 , 138 )
598
565
599
566
# create a new rectangle
600
- do_event (tool , 'press' , xdata = 10 , ydata = 10 )
601
- do_event (tool , 'onmove' , xdata = 100 , ydata = 100 )
602
- do_event (tool , 'release' , xdata = 100 , ydata = 100 )
567
+ click_and_move (tool , start = [10 , 10 ], end = [100 , 100 ])
603
568
assert tool .extents == (10 , 100 , 10 , 100 )
604
569
605
570
# Check that marker_props worked.
@@ -618,19 +583,15 @@ def onselect(vmin, vmax):
618
583
ax ._got_onselect = True
619
584
620
585
tool = widgets .RectangleSelector (ax , onselect , interactive = interactive )
621
- do_event (tool , 'press' , xdata = 100 , ydata = 110 , button = 1 )
622
586
# move outside of axis
623
- do_event (tool , 'onmove' , xdata = 150 , ydata = 120 , button = 1 )
624
- do_event (tool , 'release' , xdata = 150 , ydata = 120 , button = 1 )
587
+ click_and_move (tool , start = [100 , 110 ], end = [150 , 120 ])
625
588
626
589
assert tool .ax ._got_onselect
627
590
assert tool .extents == (100.0 , 150.0 , 110.0 , 120.0 )
628
591
629
592
# Reset tool.ax._got_onselect
630
593
tool .ax ._got_onselect = False
631
-
632
- do_event (tool , 'press' , xdata = 10 , ydata = 100 , button = 1 )
633
- do_event (tool , 'release' , xdata = 10 , ydata = 100 , button = 1 )
594
+ click_and_move (tool , start = [10 , 100 ], end = [10 , 100 ])
634
595
635
596
assert tool .ax ._got_onselect
636
597
@@ -643,19 +604,14 @@ def onselect(vmin, vmax):
643
604
644
605
tool = widgets .RectangleSelector (ax , onselect ,
645
606
ignore_event_outside = ignore_event_outside )
646
- do_event (tool , 'press' , xdata = 100 , ydata = 110 , button = 1 )
647
- do_event (tool , 'onmove' , xdata = 150 , ydata = 120 , button = 1 )
648
- do_event (tool , 'release' , xdata = 150 , ydata = 120 , button = 1 )
649
-
607
+ click_and_move (tool , start = [100 , 110 ], end = [150 , 120 ])
650
608
assert tool .ax ._got_onselect
651
609
assert tool .extents == (100.0 , 150.0 , 110.0 , 120.0 )
652
610
653
611
# Reset
654
612
ax ._got_onselect = False
655
613
# Trigger event outside of span
656
- do_event (tool , 'press' , xdata = 150 , ydata = 150 , button = 1 )
657
- do_event (tool , 'onmove' , xdata = 160 , ydata = 160 , button = 1 )
658
- do_event (tool , 'release' , xdata = 160 , ydata = 160 , button = 1 )
614
+ click_and_move (tool , start = [150 , 150 ], end = [160 , 160 ])
659
615
if ignore_event_outside :
660
616
# event have been ignored and span haven't changed.
661
617
assert not ax ._got_onselect
@@ -711,20 +667,14 @@ def onselect(vmin, vmax):
711
667
712
668
tool = widgets .SpanSelector (ax , onselect , 'horizontal' ,
713
669
interactive = interactive )
714
- do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
715
670
# move outside of axis
716
- do_event (tool , 'onmove' , xdata = 150 , ydata = 100 , button = 1 )
717
- do_event (tool , 'release' , xdata = 150 , ydata = 100 , button = 1 )
718
-
671
+ click_and_move (tool , start = [100 , 100 ], end = [150 , 100 ])
719
672
assert tool .ax ._got_onselect
720
673
assert tool .extents == (100 , 150 )
721
674
722
675
# Reset tool.ax._got_onselect
723
676
tool .ax ._got_onselect = False
724
-
725
- do_event (tool , 'press' , xdata = 10 , ydata = 100 , button = 1 )
726
- do_event (tool , 'release' , xdata = 10 , ydata = 100 , button = 1 )
727
-
677
+ click_and_move (tool , start = [10 , 100 ], end = [10 , 100 ])
728
678
assert tool .ax ._got_onselect
729
679
730
680
@@ -740,9 +690,7 @@ def onmove(vmin, vmax):
740
690
tool = widgets .SpanSelector (ax , onselect , 'horizontal' ,
741
691
onmove_callback = onmove ,
742
692
ignore_event_outside = ignore_event_outside )
743
- do_event (tool , 'press' , xdata = 100 , ydata = 100 , button = 1 )
744
- do_event (tool , 'onmove' , xdata = 125 , ydata = 125 , button = 1 )
745
- do_event (tool , 'release' , xdata = 125 , ydata = 125 , button = 1 )
693
+ click_and_move (tool , start = [100 , 100 ], end = [125 , 125 ])
746
694
assert ax ._got_onselect
747
695
assert ax ._got_on_move
748
696
assert tool .extents == (100 , 125 )
@@ -751,9 +699,7 @@ def onmove(vmin, vmax):
751
699
ax ._got_onselect = False
752
700
ax ._got_on_move = False
753
701
# Trigger event outside of span
754
- do_event (tool , 'press' , xdata = 150 , ydata = 150 , button = 1 )
755
- do_event (tool , 'onmove' , xdata = 160 , ydata = 160 , button = 1 )
756
- do_event (tool , 'release' , xdata = 160 , ydata = 160 , button = 1 )
702
+ click_and_move (tool , start = [150 , 150 ], end = [160 , 160 ])
757
703
if ignore_event_outside :
758
704
# event have been ignored and span haven't changed.
759
705
assert not ax ._got_onselect
@@ -776,9 +722,7 @@ def onselect(*args):
776
722
# Create span
777
723
tool = widgets .SpanSelector (ax , onselect , 'horizontal' , interactive = True ,
778
724
drag_from_anywhere = drag_from_anywhere )
779
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
780
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
781
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
725
+ click_and_move (tool , start = [10 , 10 ], end = [100 , 120 ])
782
726
assert tool .extents == (10 , 100 )
783
727
# Drag inside span
784
728
#
@@ -787,18 +731,14 @@ def onselect(*args):
787
731
#
788
732
# If drag_from_anywhere == False, this will create a new span with
789
733
# value extents = 25, 35
790
- do_event (tool , 'press' , xdata = 25 , ydata = 15 , button = 1 )
791
- do_event (tool , 'onmove' , xdata = 35 , ydata = 25 , button = 1 )
792
- do_event (tool , 'release' , xdata = 35 , ydata = 25 , button = 1 )
734
+ click_and_move (tool , start = [25 , 15 ], end = [35 , 25 ])
793
735
if drag_from_anywhere :
794
736
assert tool .extents == (20 , 110 )
795
737
else :
796
738
assert tool .extents == (25 , 35 )
797
739
798
740
# Check that in both cases, dragging outside the span draws a new span
799
- do_event (tool , 'press' , xdata = 175 , ydata = 185 , button = 1 )
800
- do_event (tool , 'onmove' , xdata = 185 , ydata = 195 , button = 1 )
801
- do_event (tool , 'release' , xdata = 185 , ydata = 195 , button = 1 )
741
+ click_and_move (tool , start = [175 , 185 ], end = [185 , 195 ])
802
742
assert tool .extents == (175 , 185 )
803
743
804
744
@@ -833,9 +773,7 @@ def onselect(epress, erelease):
833
773
props = dict (facecolor = 'b' , alpha = 0.2 ),
834
774
handle_props = dict (alpha = 0.5 ))
835
775
# Create rectangle
836
- do_event (tool , 'press' , xdata = 0 , ydata = 10 , button = 1 )
837
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
838
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
776
+ click_and_move (tool , start = [0 , 10 ], end = [100 , 120 ])
839
777
840
778
artist = tool ._selection_artist
841
779
assert artist .get_facecolor () == mcolors .to_rgba ('b' , alpha = 0.2 )
@@ -866,26 +804,20 @@ def onselect(*args):
866
804
Selector = widgets .RectangleSelector
867
805
868
806
tool = Selector (** kwargs )
869
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
870
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
871
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
807
+ click_and_move (tool , start = [10 , 10 ], end = [100 , 120 ])
872
808
873
809
# press-release event outside the selector to clear the selector
874
- do_event (tool , 'press' , xdata = 130 , ydata = 130 , button = 1 )
875
- do_event (tool , 'release' , xdata = 130 , ydata = 130 , button = 1 )
810
+ click_and_move (tool , start = [130 , 130 ], end = [130 , 130 ])
876
811
assert not tool ._selection_completed
877
812
878
813
ax = get_ax ()
879
814
kwargs ['ignore_event_outside' ] = True
880
815
tool = Selector (** kwargs )
881
816
assert tool .ignore_event_outside
882
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
883
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
884
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
817
+ click_and_move (tool , start = [10 , 10 ], end = [100 , 120 ])
10000
885
818
886
819
# press-release event outside the selector ignored
887
- do_event (tool , 'press' , xdata = 130 , ydata = 130 , button = 1 )
888
- do_event (tool , 'release' , xdata = 130 , ydata = 130 , button = 1 )
820
+ click_and_move (tool , start = [130 , 130 ], end = [130 , 130 ])
889
821
assert tool ._selection_completed
890
822
891
823
do_event (tool , 'on_key_press' , key = 'escape' )
@@ -905,9 +837,7 @@ def onselect(*args):
905
837
ignore_event_outside = True )
906
838
else :
907
839
tool = widgets .RectangleSelector (ax , onselect , interactive = True )
908
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
909
- do_event (tool , 'onmove' , xdata = 100 , ydata = 120 , button = 1 )
910
- do_event (tool , 'release' , xdata = 100 , ydata = 120 , button = 1 )
840
+ click_and_move (tool , start = [10 , 10 ], end = [100 , 120 ])
911
841
assert tool ._selection_completed
912
842
assert tool .visible
913
843
if selector == 'span' :
@@ -918,9 +848,7 @@ def onselect(*args):
918
848
assert not tool .visible
919
849
920
850
# Do another cycle of events to make sure we can
921
- do_event (tool , 'press' , xdata = 10 , ydata = 10 , button = 1 )
922
- do_event (tool , 'onmove' , xdata = 50 , ydata = 120 , button = 1 )
923
- do_event (tool , 'release' , xdata = 50 , ydata = 120 , button = 1 )
851
+ click_and_move (tool , start = [10 , 10 ], end = [50 , 120 ])
924
852
assert tool ._selection_completed
925
853
assert tool .visible
926
854
if selector == 'span' :
@@ -985,8 +913,7 @@ def test_span_selector_bound(direction):
985
913
press_data = [10.5 , 11.5 ]
986
914
move_data = [11 , 13 ] # Updating selector is done in onmove
987
915
release_data = move_data
988
- do_event (tool , 'press' , xdata = press_data [0 ], ydata = press_data [1 ], button = 1 )
989
- do_event (tool , 'onmove' , xdata = move_data [0 ], ydata = move_data [1 ], button = 1 )
916
+ click_and_move (tool , start = press_data , end = move_data )
990
917
991
918
assert ax .get_xbound () == x_bound
992
919
assert ax .get_ybound () == y_bound
0 commit comments