11"""
2- ==================
3- Rectangle Selector
4- ==================
2+ ===============================
3+ Rectangle and ellipse selectors
4+ ===============================
55
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.
1211"""
1312
14- from matplotlib .widgets import RectangleSelector
13+ from matplotlib .widgets import EllipseSelector , RectangleSelector
1514import numpy as np
1615import matplotlib .pyplot as plt
1716
1817
19- def line_select_callback (eclick , erelease ):
18+ def select_callback (eclick , erelease ):
2019 """
2120 Callback for line selection.
2221
@@ -25,36 +24,42 @@ def line_select_callback(eclick, erelease):
2524 x1 , y1 = eclick .xdata , eclick .ydata
2625 x2 , y2 = erelease .xdata , erelease .ydata
2726 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 } " )
2928
3029
3130def toggle_selector (event ):
32- print (' Key pressed.' )
31+ print ('Key pressed.' )
3332 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 )
4041
4142
42- fig , ax = plt .subplots ()
43+ fig = plt .figure (constrained_layout = True )
44+ axs = fig .subplots (2 )
45+
4346N = 100000 # If N is large one can see improvement by using blitting.
4447x = np .linspace (0 , 10 , N )
4548
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 ())
5863plt .show ()
5964
6065#############################################################################
@@ -65,3 +70,4 @@ def toggle_selector(event):
6570# in this example:
6671#
6772# - `matplotlib.widgets.RectangleSelector`
73+ # - `matplotlib.widgets.EllipseSelector`
0 commit comments