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

Skip to content

Commit f288658

Browse files
committed
Cleanup RectangleSelector example.
- Don't use "q" or "a" as keys given that they are already mapped to "quit" and "all_axes" (even though the latter is deprecated). - Document the interactivity directly as the axes title. - No need to plot too many things in the axes; they're irrelevant.
1 parent 969513e commit f288658

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

examples/widgets/rectangle_selector.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ def line_select_callback(eclick, erelease):
3030

3131
def toggle_selector(event):
3232
print(' Key pressed.')
33-
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
34-
print(' RectangleSelector deactivated.')
35-
toggle_selector.RS.set_active(False)
36-
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
37-
print(' RectangleSelector activated.')
38-
toggle_selector.RS.set_active(True)
33+
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)
3940

4041

4142
fig, ax = plt.subplots()
4243
N = 100000 # If N is large one can see improvement by using blitting.
43-
x = np.linspace(0.0, 10.0, N)
44+
x = np.linspace(0, 10, N)
4445

45-
ax.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
46-
ax.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
47-
ax.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
48-
49-
print("\n click --> release")
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.")
5050

5151
# drawtype is 'box' or 'line' or 'none'
5252
toggle_selector.RS = RectangleSelector(ax, line_select_callback,

lib/matplotlib/widgets.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,35 +1896,9 @@ class RectangleSelector(_SelectorWidget):
18961896
18971897
For the cursor to remain responsive you must keep a reference to it.
18981898
1899-
Example usage::
1900-
1901-
import numpy as np
1902-
import matplotlib.pyplot as plt
1903-
from matplotlib.widgets import RectangleSelector
1904-
1905-
def onselect(eclick, erelease):
1906-
"eclick and erelease are matplotlib events at press and release."
1907-
print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata))
1908-
print('endposition : (%f, %f)' % (erelease.xdata, erelease.ydata))
1909-
print('used button : ', eclick.button)
1910-
1911-
def toggle_selector(event):
1912-
print('Key pressed.')
1913-
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
1914-
print('RectangleSelector deactivated.')
1915-
toggle_selector.RS.set_active(False)
1916-
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
1917-
print('RectangleSelector activated.')
1918-
toggle_selector.RS.set_active(True)
1919-
1920-
x = np.arange(100.) / 99
1921-
y = np.sin(x)
1922-
fig, ax = plt.subplots()
1923-
ax.plot(x, y)
1924-
1925-
toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='line')
1926-
fig.canvas.mpl_connect('key_press_event', toggle_selector)
1927-
plt.show()
1899+
Examples
1900+
--------
1901+
:doc:`/gallery/widgets/rectangle_selector`
19281902
"""
19291903

19301904
_shape_klass = Rectangle

0 commit comments

Comments
 (0)