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

Skip to content

Commit feda497

Browse files
committed
Merge EllipseSelector example together with RectangleSelector.
The old EllipseSelector example had similar issues as those fixed in f288658 for RectangleSelector. I'm not too happy with the explanation text, but that can be fixed lated.
1 parent 57db5a7 commit feda497

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
@@ -3017,31 +3017,7 @@ class EllipseSelector(RectangleSelector):
30173017
30183018
Examples
30193019
--------
3020-
>>> import numpy as np
3021-
>>> import matplotlib.pyplot as plt
3022-
>>> from matplotlib.widgets import EllipseSelector
3023-
>>> def onselect(eclick, erelease):
3024-
... "eclick and erelease are matplotlib events at press and release."
3025-
... print(f'startposition: {eclick.xdata}, {eclick.ydata})
3026-
... print(f'endposition : {erelease.xdata}, {erelease.ydata})
3027-
... print('used button : ', eclick.button)
3028-
...
3029-
>>> def toggle_selector(event):
3030-
... print(' Key pressed.')
3031-
... if event.key in ['Q', 'q'] and toggle_selector.ES.active:
3032-
... print('EllipseSelector deactivated.')
3033-
... toggle_selector.RS.set_active(False)
3034-
... if event.key in ['A', 'a'] and not toggle_selector.ES.active:
3035-
... print('EllipseSelector activated.')
3036-
... toggle_selector.ES.set_active(True)
3037-
...
3038-
>>> x = np.arange(100.) / 99
3039-
>>> y = np.sin(x)
3040-
>>> fig, ax = plt.subplots()
3041-
>>> ax.plot(x, y)
3042-
>>> toggle_selector.ES = EllipseSelector(ax, onselect)
3043-
>>> fig.canvas.mpl_connect('key_press_event', toggle_selector)
3044-
>>> plt.show()
3020+
:doc:`/gallery/widgets/rectangle_selector`
30453021
"""
30463022

30473023
_shape_klass = Ellipse

0 commit comments

Comments
 (0)