forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_widgets.py
More file actions
1062 lines (836 loc) · 38 KB
/
test_widgets.py
File metadata and controls
1062 lines (836 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
import matplotlib.colors as mcolors
import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import check_figures_equal, image_comparison
from matplotlib.testing.widgets import do_event, get_ax, mock_event
from numpy.testing import assert_allclose
import pytest
def check_rectangle(**kwargs):
ax = get_ax()
def onselect(epress, erelease):
ax._got_onselect = True
assert epress.xdata == 100
assert epress.ydata == 100
assert erelease.xdata == 199
assert erelease.ydata == 199
tool = widgets.RectangleSelector(ax, onselect, **kwargs)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
do_event(tool, 'onmove', xdata=199, ydata=199, button=1)
# purposely drag outside of axis for release
do_event(tool, 'release', xdata=250, ydata=250, button=1)
if kwargs.get('drawtype', None) not in ['line', 'none']:
assert_allclose(tool.geometry,
[[100., 100, 199, 199, 100],
[100, 199, 199, 100, 100]],
err_msg=tool.geometry)
assert ax._got_onselect
def test_rectangle_selector():
check_rectangle()
with pytest.warns(
MatplotlibDeprecationWarning,
match="Support for drawtype='line' is deprecated"):
check_rectangle(drawtype='line', useblit=False)
check_rectangle(useblit=True, button=1)
with pytest.warns(
MatplotlibDeprecationWarning,
match="Support for drawtype='none' is deprecated"):
check_rectangle(drawtype='none', minspanx=10, minspany=10)
check_rectangle(minspanx=10, minspany=10, spancoords='pixels')
check_rectangle(props=dict(fill=True))
@pytest.mark.parametrize('drag_from_anywhere, new_center',
[[True, (60, 75)],
[False, (30, 20)]])
def test_rectangle_drag(drag_from_anywhere, new_center):
ax = get_ax()
def onselect(epress, erelease):
pass
tool = widgets.RectangleSelector(ax, onselect, interactive=True,
drag_from_anywhere=drag_from_anywhere)
# Create rectangle
do_event(tool, 'press', xdata=0, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
assert tool.center == (50, 65)
# Drag inside rectangle, but away from centre handle
#
# If drag_from_anywhere == True, this will move the rectangle by (10, 10),
# giving it a new center of (60, 75)
#
# If drag_from_anywhere == False, this will create a new rectangle with
# center (30, 20)
do_event(tool, 'press', xdata=25, ydata=15, button=1)
do_event(tool, 'onmove', xdata=35, ydata=25, button=1)
do_event(tool, 'release', xdata=35, ydata=25, button=1)
assert tool.center == new_center
# Check that in both cases, dragging outside the rectangle draws a new
# rectangle
do_event(tool, 'press', xdata=175, ydata=185, button=1)
do_event(tool, 'onmove', xdata=185, ydata=195, button=1)
do_event(tool, 'release', xdata=185, ydata=195, button=1)
assert tool.center == (180, 190)
def test_rectangle_selector_set_props_handle_props():
ax = get_ax()
def onselect(epress, erelease):
pass
tool = widgets.RectangleSelector(ax, onselect, interactive=True,
props=dict(facecolor='b', alpha=0.2),
handle_props=dict(alpha=0.5))
# Create rectangle
do_event(tool, 'press', xdata=0, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
artist = tool._selection_artist
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
tool.set_props(facecolor='r', alpha=0.3)
assert artist.get_facecolor() == mcolors.to_rgba('r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_markeredgecolor() == 'black'
assert artist.get_alpha() == 0.5
tool.set_handle_props(markeredgecolor='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_markeredgecolor() == 'r'
assert artist.get_alpha() == 0.3
def test_ellipse():
"""For ellipse, test out the key modifiers"""
ax = get_ax()
def onselect(epress, erelease):
pass
tool = widgets.EllipseSelector(ax, onselect=onselect,
grab_range=10, interactive=True)
tool.extents = (100, 150, 100, 150)
# drag the rectangle
do_event(tool, 'press', xdata=10, ydata=10, button=1,
key=' ')
do_event(tool, 'onmove', xdata=30, ydata=30, button=1)
do_event(tool, 'release', xdata=30, ydata=30, button=1)
assert tool.extents == (120, 170, 120, 170)
# create from center
do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1,
key='control')
do_event(tool, 'press', xdata=100, ydata=100, button=1)
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
do_event(tool, 'release', xdata=125, ydata=125, button=1)
do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1,
key='control')
assert tool.extents == (75, 125, 75, 125)
# create a square
do_event(tool, 'on_key_press', xdata=10, ydata=10, button=1,
key='shift')
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=35, ydata=30, button=1)
do_event(tool, 'release', xdata=35, ydata=30, button=1)
do_event(tool, 'on_key_release', xdata=10, ydata=10, button=1,
key='shift')
extents = [int(e) for e in tool.extents]
assert extents == [10, 35, 10, 34]
# create a square from center
do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1,
key='ctrl+shift')
do_event(tool, 'press', xdata=100, ydata=100, button=1)
do_event(tool, 'onmove', xdata=125, ydata=130, button=1)
do_event(tool, 'release', xdata=125, ydata=130, button=1)
do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1,
key='ctrl+shift')
extents = [int(e) for e in tool.extents]
assert extents == [70, 129, 70, 130]
assert tool.geometry.shape == (2, 73)
assert_allclose(tool.geometry[:, 0], [70., 100])
def test_rectangle_handles():
ax = get_ax()
def onselect(epress, erelease):
pass
tool = widgets.RectangleSelector(ax, onselect=onselect,
grab_range=10,
interactive=True,
handle_props={'markerfacecolor': 'r',
'markeredgecolor': 'b'})
tool.extents = (100, 150, 100, 150)
assert tool.corners == (
(100, 150, 150, 100), (100, 100, 150, 150))
assert tool.extents == (100, 150, 100, 150)
assert tool.edge_centers == (
(100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150))
assert tool.extents == (100, 150, 100, 150)
# grab a corner and move it
do_event(tool, 'press', xdata=100, ydata=100)
do_event(tool, 'onmove', xdata=120, ydata=120)
do_event(tool, 'release', xdata=120, ydata=120)
assert tool.extents == (120, 150, 120, 150)
# grab the center and move it
do_event(tool, 'press', xdata=132, ydata=132)
do_event(tool, 'onmove', xdata=120, ydata=120)
do_event(tool, 'release', xdata=120, ydata=120)
assert tool.extents == (108, 138, 108, 138)
# create a new rectangle
do_event(tool, 'press', xdata=10, ydata=10)
do_event(tool, 'onmove', xdata=100, ydata=100)
do_event(tool, 'release', xdata=100, ydata=100)
assert tool.extents == (10, 100, 10, 100)
# Check that marker_props worked.
assert mcolors.same_color(
tool._corner_handles.artists[0].get_markerfacecolor(), 'r')
assert mcolors.same_color(
tool._corner_handles.artists[0].get_markeredgecolor(), 'b')
@pytest.mark.parametrize('interactive', [True, False])
def test_rectangle_selector_onselect(interactive):
# check when press and release events take place at the same position
ax = get_ax()
def onselect(vmin, vmax):
ax._got_onselect = True
tool = widgets.RectangleSelector(ax, onselect, interactive=interactive)
do_event(tool, 'press', xdata=100, ydata=110, button=1)
# move outside of axis
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
do_event(tool, 'release', xdata=150, ydata=120, button=1)
assert tool.ax._got_onselect
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
# Reset tool.ax._got_onselect
tool.ax._got_onselect = False
do_event(tool, 'press', xdata=10, ydata=100, button=1)
do_event(tool, 'release', xdata=10, ydata=100, button=1)
assert tool.ax._got_onselect
@pytest.mark.parametrize('ignore_event_outside', [True, False])
def test_rectangle_selector_ignore_outside(ignore_event_outside):
ax = get_ax()
def onselect(vmin, vmax):
ax._got_onselect = True
tool = widgets.RectangleSelector(ax, onselect,
ignore_event_outside=ignore_event_outside)
do_event(tool, 'press', xdata=100, ydata=110, button=1)
do_event(tool, 'onmove', xdata=150, ydata=120, button=1)
do_event(tool, 'release', xdata=150, ydata=120, button=1)
assert tool.ax._got_onselect
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
# Reset
ax._got_onselect = False
# Trigger event outside of span
do_event(tool, 'press', xdata=150, ydata=150, button=1)
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
do_event(tool, 'release', xdata=160, ydata=160, button=1)
if ignore_event_outside:
# event have been ignored and span haven't changed.
assert not ax._got_onselect
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
else:
# A new shape is created
assert ax._got_onselect
assert tool.extents == (150.0, 160.0, 150.0, 160.0)
def check_span(*args, **kwargs):
ax = get_ax()
def onselect(vmin, vmax):
ax._got_onselect = True
assert vmin == 100
assert vmax == 199
def onmove(vmin, vmax):
assert vmin == 100
assert vmax == 199
ax._got_on_move = True
if 'onmove_callback' in kwargs:
kwargs['onmove_callback'] = onmove
tool = widgets.SpanSelector(ax, onselect, *args, **kwargs)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
# move outside of axis
do_event(tool, 'onmove', xdata=199, ydata=199, button=1)
do_event(tool, 'release', xdata=250, ydata=250, button=1)
assert ax._got_onselect
if 'onmove_callback' in kwargs:
assert ax._got_on_move
def test_span_selector():
check_span('horizontal', minspan=10, useblit=True)
check_span('vertical', onmove_callback=True, button=1)
check_span('horizontal', props=dict(fill=True))
check_span('horizontal', interactive=True)
@pytest.mark.parametrize('interactive', [True, False])
def test_span_selector_onselect(interactive):
# check when press and release events take place at the same position
ax = get_ax()
def onselect(vmin, vmax):
ax._got_onselect = True
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
interactive=interactive)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
# move outside of axis
do_event(tool, 'onmove', xdata=150, ydata=100, button=1)
do_event(tool, 'release', xdata=150, ydata=100, button=1)
assert tool.ax._got_onselect
assert tool.extents == (100, 150)
# Reset tool.ax._got_onselect
tool.ax._got_onselect = False
do_event(tool, 'press', xdata=10, ydata=100, button=1)
do_event(tool, 'release', xdata=10, ydata=100, button=1)
assert tool.ax._got_onselect
@pytest.mark.parametrize('ignore_event_outside', [True, False])
def test_span_selector_ignore_outside(ignore_event_outside):
ax = get_ax()
def onselect(vmin, vmax):
ax._got_onselect = True
def onmove(vmin, vmax):
ax._got_on_move = True
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
onmove_callback=onmove,
ignore_event_outside=ignore_event_outside)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
do_event(tool, 'release', xdata=125, ydata=125, button=1)
assert ax._got_onselect
assert ax._got_on_move
assert tool.extents == (100, 125)
# Reset
ax._got_onselect = False
ax._got_on_move = False
# Trigger event outside of span
do_event(tool, 'press', xdata=150, ydata=150, button=1)
do_event(tool, 'onmove', xdata=160, ydata=160, button=1)
do_event(tool, 'release', xdata=160, ydata=160, button=1)
if ignore_event_outside:
# event have been ignored and span haven't changed.
assert not ax._got_onselect
assert not ax._got_on_move
assert tool.extents == (100, 125)
else:
# A new shape is created
assert ax._got_onselect
assert ax._got_on_move
assert tool.extents == (150, 160)
@pytest.mark.parametrize('drag_from_anywhere', [True, False])
def test_span_selector_drag(drag_from_anywhere):
ax = get_ax()
def onselect(*args):
pass
# Create span
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True,
drag_from_anywhere=drag_from_anywhere)
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
assert tool.extents == (10, 100)
# Drag inside span
#
# If drag_from_anywhere == True, this will move the span by 10,
# giving new value extents = 20, 110
#
# If drag_from_anywhere == False, this will create a new span with
# value extents = 25, 35
do_event(tool, 'press', xdata=25, ydata=15, button=1)
do_event(tool, 'onmove', xdata=35, ydata=25, button=1)
do_event(tool, 'release', xdata=35, ydata=25, button=1)
if drag_from_anywhere:
assert tool.extents == (20, 110)
else:
assert tool.extents == (25, 35)
# Check that in both cases, dragging outside the span draws a new span
do_event(tool, 'press', xdata=175, ydata=185, button=1)
do_event(tool, 'onmove', xdata=185, ydata=195, button=1)
do_event(tool, 'release', xdata=185, ydata=195, button=1)
assert tool.extents == (175, 185)
def test_span_selector_direction():
ax = get_ax()
def onselect(*args):
pass
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True)
assert tool.direction == 'horizontal'
assert tool._edge_handles.direction == 'horizontal'
with pytest.raises(ValueError):
tool = widgets.SpanSelector(ax, onselect, 'invalid_direction')
tool.direction = 'vertical'
assert tool.direction == 'vertical'
assert tool._edge_handles.direction == 'vertical'
with pytest.raises(ValueError):
tool.direction = 'invalid_string'
def test_span_selector_set_props_handle_props():
ax = get_ax()
def onselect(epress, erelease):
pass
tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True,
props=dict(facecolor='b', alpha=0.2),
handle_props=dict(alpha=0.5))
# Create rectangle
do_event(tool, 'press', xdata=0, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
artist = tool._selection_artist
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
tool.set_props(facecolor='r', alpha=0.3)
assert artist.get_facecolor() == mcolors.to_rgba('r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.5
tool.set_handle_props(color='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3
@pytest.mark.parametrize('selector', ['span', 'rectangle'])
def test_selector_clear(selector):
ax = get_ax()
def onselect(*args):
pass
kwargs = dict(ax=ax, onselect=onselect, interactive=True)
if selector == 'span':
Selector = widgets.SpanSelector
kwargs['direction'] = 'horizontal'
else:
Selector = widgets.RectangleSelector
tool = Selector(**kwargs)
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
# press-release event outside the selector to clear the selector
do_event(tool, 'press', xdata=130, ydata=130, button=1)
do_event(tool, 'release', xdata=130, ydata=130, button=1)
assert not tool._selection_completed
ax = get_ax()
kwargs['ignore_event_outside'] = True
tool = Selector(**kwargs)
assert tool.ignore_event_outside
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
# press-release event outside the selector ignored
do_event(tool, 'press', xdata=130, ydata=130, button=1)
do_event(tool, 'release', xdata=130, ydata=130, button=1)
assert tool._selection_completed
do_event(tool, 'on_key_press', key='escape')
assert not tool._selection_completed
@pytest.mark.parametrize('selector', ['span', 'rectangle'])
def test_selector_clear_method(selector):
ax = get_ax()
def onselect(*args):
pass
if selector == 'span':
tool = widgets.SpanSelector(ax, onselect, 'horizontal',
interactive=True,
ignore_event_outside=True)
else:
tool = widgets.RectangleSelector(ax, onselect, interactive=True)
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)
assert tool._selection_completed
assert tool.visible
if selector == 'span':
assert tool.extents == (10, 100)
tool.clear()
assert not tool._selection_completed
assert not tool.visible
# Do another cycle of events to make sure we can
do_event(tool, 'press', xdata=10, ydata=10, button=1)
do_event(tool, 'onmove', xdata=50, ydata=120, button=1)
do_event(tool, 'release', xdata=50, ydata=120, button=1)
assert tool._selection_completed
assert tool.visible
if selector == 'span':
assert tool.extents == (10, 50)
def test_tool_line_handle():
ax = get_ax()
positions = [20, 30, 50]
tool_line_handle = widgets.ToolLineHandles(ax, positions, 'horizontal',
useblit=False)
for artist in tool_line_handle.artists:
assert not artist.get_animated()
assert not artist.get_visible()
tool_line_handle.set_visible(True)
tool_line_handle.set_animated(True)
for artist in tool_line_handle.artists:
assert artist.get_animated()
assert artist.get_visible()
assert tool_line_handle.positions == positions
@pytest.mark.parametrize('direction', ("horizontal", "vertical"))
def test_span_selector_bound(direction):
fig, ax = plt.subplots(1, 1)
ax.plot([10, 20], [10, 30])
ax.figure.canvas.draw()
x_bound = ax.get_xbound()
y_bound = ax.get_ybound()
tool = widgets.SpanSelector(ax, print, direction, interactive=True)
assert ax.get_xbound() == x_bound
assert ax.get_ybound() == y_bound
bound = x_bound if direction == 'horizontal' else y_bound
assert tool._edge_handles.positions == list(bound)
press_data = [10.5, 11.5]
move_data = [11, 13] # Updating selector is done in onmove
release_data = move_data
do_event(tool, 'press', xdata=press_data[0], ydata=press_data[1], button=1)
do_event(tool, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1)
assert ax.get_xbound() == x_bound
assert ax.get_ybound() == y_bound
index = 0 if direction == 'horizontal' else 1
handle_positions = [press_data[index], release_data[index]]
assert tool._edge_handles.positions == handle_positions
def check_lasso_selector(**kwargs):
ax = get_ax()
def onselect(verts):
ax._got_onselect = True
assert verts == [(100, 100), (125, 125), (150, 150)]
tool = widgets.LassoSelector(ax, onselect, **kwargs)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
do_event(tool, 'onmove', xdata=125, ydata=125, button=1)
do_event(tool, 'release', xdata=150, ydata=150, button=1)
assert ax._got_onselect
def test_lasso_selector():
check_lasso_selector()
check_lasso_selector(useblit=False, props=dict(color='red'))
check_lasso_selector(useblit=True, button=1)
def test_CheckButtons():
ax = get_ax()
check = widgets.CheckButtons(ax, ('a', 'b', 'c'), (True, False, True))
assert check.get_status() == [True, False, True]
check.set_active(0)
assert check.get_status() == [False, False, True]
cid = check.on_clicked(lambda: None)
check.disconnect(cid)
@pytest.mark.parametrize("toolbar", ["none", "toolbar2", "toolmanager"])
def test_TextBox(toolbar):
# Avoid "toolmanager is provisional" warning.
dict.__setitem__(plt.rcParams, "toolbar", toolbar)
from unittest.mock import Mock
submit_event = Mock()
text_change_event = Mock()
ax = get_ax()
tool = widgets.TextBox(ax, '')
tool.on_submit(submit_event)
tool.on_text_change(text_change_event)
assert tool.text == ''
do_event(tool, '_click')
tool.set_val('x**2')
assert tool.text == 'x**2'
assert text_change_event.call_count == 1
tool.begin_typing(tool.text)
tool.stop_typing()
assert submit_event.call_count == 2
do_event(tool, '_click')
do_event(tool, '_keypress', key='+')
do_event(tool, '_keypress', key='5')
assert text_change_event.call_count == 3
@image_comparison(['check_radio_buttons.png'], style='mpl20', remove_text=True)
def test_check_radio_buttons_image():
# Remove this line when this test image is regenerated.
plt.rcParams['text.kerning_factor'] = 6
get_ax()
plt.subplots_adjust(left=0.3)
rax1 = plt.axes([0.05, 0.7, 0.15, 0.15])
rax2 = plt.axes([0.05, 0.2, 0.15, 0.15])
widgets.RadioButtons(rax1, ('Radio 1', 'Radio 2', 'Radio 3'))
widgets.CheckButtons(rax2, ('Check 1', 'Check 2', 'Check 3'),
(False, True, True))
@image_comparison(['check_bunch_of_radio_buttons.png'],
style='mpl20', remove_text=True)
def test_check_bunch_of_radio_buttons():
rax = plt.axes([0.05, 0.1, 0.15, 0.7])
widgets.RadioButtons(rax, ('B1', 'B2', 'B3', 'B4', 'B5', 'B6',
'B7', 'B8', 'B9', 'B10', 'B11', 'B12',
'B13', 'B14', 'B15'))
def test_slider_slidermin_slidermax_invalid():
fig, ax = plt.subplots()
# test min/max with floats
with pytest.raises(ValueError):
widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
slidermin=10.0)
with pytest.raises(ValueError):
widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
slidermax=10.0)
def test_slider_slidermin_slidermax():
fig, ax = plt.subplots()
slider_ = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=5.0)
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=1.0, slidermin=slider_)
assert slider.val == slider_.val
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=10.0, slidermax=slider_)
assert slider.val == slider_.val
def test_slider_valmin_valmax():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=-10.0)
assert slider.val == slider.valmin
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=25.0)
assert slider.val == slider.valmax
def test_slider_valstep_snapping():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=11.4, valstep=1)
assert slider.val == 11
slider = widgets.Slider(ax=ax, label='', valmin=0.0, valmax=24.0,
valinit=11.4, valstep=[0, 1, 5.5, 19.7])
assert slider.val == 5.5
def test_slider_horizontal_vertical():
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0, valmax=24,
valinit=12, orientation='horizontal')
slider.set_val(10)
assert slider.val == 10
# check the dimension of the slider patch in axes units
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.bounds, [0, .25, 10/24, .5])
fig, ax = plt.subplots()
slider = widgets.Slider(ax=ax, label='', valmin=0, valmax=24,
valinit=12, orientation='vertical')
slider.set_val(10)
assert slider.val == 10
# check the dimension of the slider patch in axes units
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.bounds, [.25, 0, .5, 10/24])
@pytest.mark.parametrize("orientation", ["horizontal", "vertical"])
def test_range_slider(orientation):
if orientation == "vertical":
idx = [1, 0, 3, 2]
else:
idx = [0, 1, 2, 3]
fig, ax = plt.subplots()
slider = widgets.RangeSlider(
ax=ax, label="", valmin=0.0, valmax=1.0, orientation=orientation,
valinit=[0.1, 0.34]
)
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.get_points().flatten()[idx], [0.1, 0.25, 0.34, 0.75])
# Check initial value is set correctly
assert_allclose(slider.val, (0.1, 0.34))
slider.set_val((0.2, 0.6))
assert_allclose(slider.val, (0.2, 0.6))
box = slider.poly.get_extents().transformed(ax.transAxes.inverted())
assert_allclose(box.get_points().flatten()[idx], [0.2, .25, 0.6, .75])
slider.set_val((0.2, 0.1))
assert_allclose(slider.val, (0.1, 0.2))
slider.set_val((-1, 10))
assert_allclose(slider.val, (0, 1))
def check_polygon_selector(event_sequence, expected_result, selections_count):
"""
Helper function to test Polygon Selector.
Parameters
----------
event_sequence : list of tuples (etype, dict())
A sequence of events to perform. The sequence is a list of tuples
where the first element of the tuple is an etype (e.g., 'onmove',
'press', etc.), and the second element of the tuple is a dictionary of
the arguments for the event (e.g., xdata=5, key='shift', etc.).
expected_result : list of vertices (xdata, ydata)
The list of vertices that are expected to result from the event
sequence.
selections_count : int
Wait for the tool to call its `onselect` function `selections_count`
times, before comparing the result to the `expected_result`
"""
ax = get_ax()
ax._selections_count = 0
def onselect(vertices):
ax._selections_count += 1
ax._current_result = vertices
tool = widgets.PolygonSelector(ax, onselect)
for (etype, event_args) in event_sequence:
do_event(tool, etype, **event_args)
assert ax._selections_count == selections_count
assert ax._current_result == expected_result
def polygon_place_vertex(xdata, ydata):
return [('onmove', dict(xdata=xdata, ydata=ydata)),
('press', dict(xdata=xdata, ydata=ydata)),
('release', dict(xdata=xdata, ydata=ydata))]
def polygon_remove_vertex(xdata, ydata):
return [('onmove', dict(xdata=xdata, ydata=ydata)),
('press', dict(xdata=xdata, ydata=ydata, button=3)),
('release', dict(xdata=xdata, ydata=ydata, button=3))]
def test_polygon_selector():
# Simple polygon
expected_result = [(50, 50), (150, 50), (50, 150)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))
check_polygon_selector(event_sequence, expected_result, 1)
# Move first vertex before completing the polygon.
expected_result = [(75, 50), (150, 50), (50, 150)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ [('on_key_press', dict(key='control')),
('onmove', dict(xdata=50, ydata=50)),
('press', dict(xdata=50, ydata=50)),
('onmove', dict(xdata=75, ydata=50)),
('release', dict(xdata=75, ydata=50)),
('on_key_release', dict(key='control'))]
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(75, 50))
check_polygon_selector(event_sequence, expected_result, 1)
# Move first two vertices at once before completing the polygon.
expected_result = [(50, 75), (150, 75), (50, 150)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ [('on_key_press', dict(key='shift')),
('onmove', dict(xdata=100, ydata=100)),
('press', dict(xdata=100, ydata=100)),
('onmove', dict(xdata=100, ydata=125)),
('release', dict(xdata=100, ydata=125)),
('on_key_release', dict(key='shift'))]
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 75))
check_polygon_selector(event_sequence, expected_result, 1)
# Move first vertex after completing the polygon.
expected_result = [(75, 50), (150, 50), (50, 150)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50)
+ [('onmove', dict(xdata=50, ydata=50)),
('press', dict(xdata=50, ydata=50)),
('onmove', dict(xdata=75, ydata=50)),
('release', dict(xdata=75, ydata=50))])
check_polygon_selector(event_sequence, expected_result, 2)
# Move all vertices after completing the polygon.
expected_result = [(75, 75), (175, 75), (75, 175)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50)
+ [('on_key_press', dict(key='shift')),
('onmove', dict(xdata=100, ydata=100)),
('press', dict(xdata=100, ydata=100)),
('onmove', dict(xdata=125, ydata=125)),
('release', dict(xdata=125, ydata=125)),
('on_key_release', dict(key='shift'))])
check_polygon_selector(event_sequence, expected_result, 2)
# Try to move a vertex and move all before placing any vertices.
expected_result = [(50, 50), (150, 50), (50, 150)]
event_sequence = ([('on_key_press', dict(key='control')),
('onmove', dict(xdata=100, ydata=100)),
('press', dict(xdata=100, ydata=100)),
('onmove', dict(xdata=125, ydata=125)),
('release', dict(xdata=125, ydata=125)),
('on_key_release', dict(key='control')),
('on_key_press', dict(key='shift')),
('onmove', dict(xdata=100, ydata=100)),
('press', dict(xdata=100, ydata=100)),
('onmove', dict(xdata=125, ydata=125)),
('release', dict(xdata=125, ydata=125)),
('on_key_release', dict(key='shift'))]
+ polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))
check_polygon_selector(event_sequence, expected_result, 1)
# Try to place vertex out-of-bounds, then reset, and start a new polygon.
expected_result = [(50, 50), (150, 50), (50, 150)]
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(250, 50)
+ [('on_key_press', dict(key='escape')),
('on_key_release', dict(key='escape'))]
+ polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))
check_polygon_selector(event_sequence, expected_result, 1)
def test_polygon_selector_set_props_handle_props():
ax = get_ax()
ax._selections_count = 0
def onselect(vertices):
ax._selections_count += 1
ax._current_result = vertices
tool = widgets.PolygonSelector(ax, onselect,
props=dict(color='b', alpha=0.2),
handle_props=dict(alpha=0.5))
event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))
for (etype, event_args) in event_sequence:
do_event(tool, etype, **event_args)
artist = tool._selection_artist
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.2
tool.set_props(color='r', alpha=0.3)
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3
for artist in tool._handles_artists:
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.5
tool.set_handle_props(color='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3
@pytest.mark.parametrize(
"horizOn, vertOn",
[(True, True), (True, False), (False, True)],
)
def test_MultiCursor(horizOn, vertOn):
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
# useblit=false to avoid having to draw the figure to cache the renderer
multi = widgets.MultiCursor(
fig.canvas, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
)
# Only two of the axes should have a line drawn on them.
if vertOn:
assert len(multi.vlines) == 2
if horizOn:
assert len(multi.hlines) == 2
# mock a motion_notify_event
# Can't use `do_event` as that helper requires the widget
# to have a single .ax attribute.
event = mock_event(ax1, xdata=.5, ydata=.25)
multi.onmove(event)
# the lines in the first two ax should both move
for l in multi.vlines:
assert l.get_xdata() == (.5, .5)
for l in multi.hlines:
assert l.get_ydata() == (.25, .25)
# test a move event in an axes not part of the MultiCursor
# the lines in ax1 and ax2 should not have moved.
event = mock_event(ax3, xdata=.75, ydata=.75)
multi.onmove(event)
for l in multi.vlines:
assert l.get_xdata() == (.5, .5)
for l in multi.hlines:
assert l.get_ydata() == (.25, .25)
@check_figures_equal()
def test_rect_visibility(fig_test, fig_ref):
# Check that requesting an invisible selector makes it invisible
ax_test = fig_test.subplots()