Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a46327

Browse files
authored
Merge pull request #20602 from anntzer/es
Merge EllipseSelector example together with RectangleSelector.
2 parents 0cf55c1 + feda497 commit 3a46327

File tree

2 files changed

+39
-57
lines changed

2 files changed

+39
-57
lines changed
Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
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
1514
import numpy as np
1615
import 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

3130
def 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+
4346
N = 100000 # If N is large one can see improvement by using blitting.
4447
x = 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())
5863
plt.show()
5964

6065
#############################################################################
@@ -65,3 +70,4 @@ def toggle_selector(event):
6570
# in this example:
6671
#
6772
# - `matplotlib.widgets.RectangleSelector`
73+
# - `matplotlib.widgets.EllipseSelector`

lib/matplotlib/widgets.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,31 +3052,7 @@ class EllipseSelector(RectangleSelector):
30523052
30533053
Examples
30543054
--------
3055-
>>> import numpy as np
3056-
>>> import matplotlib.pyplot as plt
3057-
>>> from matplotlib.widgets import EllipseSelector
3058-
>>> def onselect(eclick, erelease):
3059-
... "eclick and erelease are matplotlib events at press and release."
3060-
... print(f'startposition: {eclick.xdata}, {eclick.ydata})
3061-
... print(f'endposition : {erelease.xdata}, {erelease.ydata})
3062-
... print('used button : ', eclick.button)
3063-
...
3064-
>>> def toggle_selector(event):
3065-
... print(' Key pressed.')
3066-
... if event.key in ['Q', 'q'] and toggle_selector.ES.active:
3067-
... print('EllipseSelector deactivated.')
3068-
... toggle_selector.RS.set_active(False)
3069-
... if event.key in ['A', 'a'] and not toggle_selector.ES.active:
3070-
... print('EllipseSelector activated.')
3071-
... toggle_selector.ES.set_active(True)
3072-
...
3073-
>>> x = np.arange(100.) / 99
3074-
>>> y = np.sin(x)
3075-
>>> fig, ax = plt.subplots()
3076-
>>> ax.plot(x, y)
3077-
>>> toggle_selector.ES = EllipseSelector(ax, onselect)
3078-
>>> fig.canvas.mpl_connect('key_press_event', toggle_selector)
3079-
>>> plt.show()
3055+
:doc:`/gallery/widgets/rectangle_selector`
30803056
"""
30813057

30823058
_shape_klass = Ellipse

0 commit comments

Comments
 (0)