1
1
"""
2
- ==================
3
- Rectangle Selector
4
- ==================
2
+ ===============================
3
+ Rectangle and ellipse selectors
4
+ ===============================
5
5
6
- Do a mouseclick somewhere, move the mouse to some destination, release
7
- the button. This class gives click- and release-events and also draws
8
- a line or a box from the click-point to the actual mouseposition
9
- (within the same axes) until the button is released. Within the
10
- method ``self.ignore()`` it is checked whether the button from eventpress
11
- and eventrelease are the same.
6
+ Click somewhere, move the mouse, and release the mouse button.
7
+ `.RectangleSelector` and `.EllipseSelector` draw a rectangle or an ellipse
8
+ from the initial click position to the current mouse position (within the same
9
+ axes) until the button is released. A connected callback receives the click-
10
+ and release-events.
12
11
"""
13
12
14
- from matplotlib .widgets import RectangleSelector
13
+ from matplotlib .widgets import EllipseSelector , RectangleSelector
15
14
import numpy as np
16
15
import matplotlib .pyplot as plt
17
16
18
17
19
- def line_select_callback (eclick , erelease ):
18
+ def select_callback (eclick , erelease ):
20
19
"""
21
20
Callback for line selection.
22
21
@@ -25,36 +24,42 @@ def line_select_callback(eclick, erelease):
25
24
x1 , y1 = eclick .xdata , eclick .ydata
26
25
x2 , y2 = erelease .xdata , erelease .ydata
27
26
print (f"({ x1 :3.2f} , { y1 :3.2f} ) --> ({ x2 :3.2f} , { y2 :3.2f} )" )
28
- print (f" The buttons you used were: { eclick .button } { erelease .button } " )
27
+ print (f"The buttons you used were: { eclick .button } { erelease .button } " )
29
28
30
29
31
30
def toggle_selector (event ):
32
- print (' Key pressed.' )
31
+ print ('Key pressed.' )
33
32
if event .key == 't' :
34
- if toggle_selector .RS .active :
35
- print (' RectangleSelector deactivated.' )
36
- toggle_selector .RS .set_active (False )
37
- else :
38
- print (' RectangleSelector activated.' )
39
- toggle_selector .RS .set_active (True )
33
+ for selector in selectors :
34
+ name = type (selector ).__name__
35
+ if selector .active :
36
+ print (f'{ name } deactivated.' )
37
+ selector .set_active (False )
38
+ else :
39
+ print (f'{ name } activated.' )
40
+ selector .set_active (True )
40
41
41
42
42
- fig , ax = plt .subplots ()
43
+ fig = plt .figure (constrained_layout = True )
44
+ axs = fig .subplots (2 )
45
+
43
46
N = 100000 # If N is large one can see improvement by using blitting.
44
47
x = np .linspace (0 , 10 , N )
45
48
46
- ax .plot (x , np .sin (2 * np .pi * x )) # plot something
47
- ax .set_title (
48
- "Click and drag to draw a rectangle.\n "
49
- "Press 't' to toggle the selector on and off." )
50
-
51
- toggle_selector .RS = RectangleSelector (ax , line_select_callback ,
52
- useblit = True ,
53
- button = [1 , 3 ], # disable middle button
54
- minspanx = 5 , minspany = 5 ,
55
- spancoords = 'pixels' ,
56
- interactive = True )
57
- fig .canvas .mpl_connect ('key_press_event' , toggle_selector )
49
+ selectors = []
50
+ for ax , selector_class in zip (axs , [RectangleSelector , EllipseSelector ]):
51
+ ax .plot (x , np .sin (2 * np .pi * x )) # plot something
52
+ ax .set_title (f"Click and drag to draw a { selector_class .__name__ } ." )
53
+ selectors .append (selector_class (
54
+ ax , select_callback ,
55
+ useblit = True ,
56
+ button = [1 , 3 ], # disable middle button
57
+ minspanx = 5 , minspany = 5 ,
58
+ spancoords = 'pixels' ,
59
+ interactive = True ))
60
+ fig .canvas .mpl_connect ('key_press_event' , toggle_selector )
61
+ axs [0 ].set_title ("Press 't' to toggle the selectors on and off.\n "
62
+ + axs [0 ].get_title ())
58
63
plt .show ()
59
64
60
65
#############################################################################
@@ -65,3 +70,4 @@ def toggle_selector(event):
65
70
# in this example:
66
71
#
67
72
# - `matplotlib.widgets.RectangleSelector`
73
+ # - `matplotlib.widgets.EllipseSelector`
0 commit comments