Skip to content

Commit 9ed4cac

Browse files
committed
Add example to RectangleSelector docstring and improve EllipseSelector docstring
1 parent f1d7ac8 commit 9ed4cac

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

lib/matplotlib/widgets.py

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,12 +2498,8 @@ def closest(self, x, y):
24982498
return min_index, dist[min_index]
24992499

25002500

2501-
class RectangleSelector(_SelectorWidget):
2501+
_RECTANGLESELECTOR_PARAMETERS_DOCSTRING = \
25022502
r"""
2503-
Select a rectangular region of an axes.
2504-
2505-
For the cursor to remain responsive you must keep a reference to it.
2506-
25072503
Parameters
25082504
----------
25092505
ax : `~matplotlib.axes.Axes`
@@ -2577,11 +2573,34 @@ def onselect(eclick: MouseEvent, erelease: MouseEvent)
25772573
drag_from_anywhere : bool, optional
25782574
If `True`, the widget can be moved by clicking anywhere within
25792575
its bounds.
2576+
"""
2577+
2578+
2579+
class RectangleSelector(_SelectorWidget):
2580+
"""
2581+
Select a rectangular region of an axes.
2582+
2583+
For the cursor to remain responsive you must keep a reference to it.
2584+
2585+
%s
25802586
25812587
Examples
25822588
--------
2583-
:doc:`/gallery/widgets/rectangle_selector`
2589+
>>> import matplotlib.pyplot as plt
2590+
>>> import matplotlib.widgets as mwidgets
2591+
>>> fig, ax = plt.subplots()
2592+
>>> ax.plot([1, 2, 3], [10, 50, 100])
2593+
>>> def onselect(eclick, erelease):
2594+
... print(eclick.xdata, eclick.ydata)
2595+
... print(erelease.xdata, erelease.ydata)
2596+
>>> rectprops = dict(facecolor='blue', alpha=0.5)
2597+
>>> rect = mwidgets.RectangleSelector(ax, onselect, rectprops=rectprops,
2598+
interactive=True)
2599+
>>> fig.show()
2600+
2601+
See also: :doc:`/gallery/widgets/rectangle_selector`
25842602
"""
2603+
__doc__ %= (_RECTANGLESELECTOR_PARAMETERS_DOCSTRING)
25852604

25862605
_shape_klass = Rectangle
25872606

@@ -2955,36 +2974,38 @@ class EllipseSelector(RectangleSelector):
29552974
29562975
For the cursor to remain responsive you must keep a reference to it.
29572976
2958-
Example usage::
2959-
2960-
import numpy as np
2961-
import matplotlib.pyplot as plt
2962-
from matplotlib.widgets import EllipseSelector
2963-
2964-
def onselect(eclick, erelease):
2965-
"eclick and erelease are matplotlib events at press and release."
2966-
print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata))
2967-
print('endposition : (%f, %f)' % (erelease.xdata, erelease.ydata))
2968-
print('used button : ', eclick.button)
2969-
2970-
def toggle_selector(event):
2971-
print(' Key pressed.')
2972-
if event.key in ['Q', 'q'] and toggle_selector.ES.active:
2973-
print('EllipseSelector deactivated.')
2974-
toggle_selector.RS.set_active(False)
2975-
if event.key in ['A', 'a'] and not toggle_selector.ES.active:
2976-
print('EllipseSelector activated.')
2977-
toggle_selector.ES.set_active(True)
2978-
2979-
x = np.arange(100.) / 99
2980-
y = np.sin(x)
2981-
fig, ax = plt.subplots()
2982-
ax.plot(x, y)
2977+
%s
29832978
2984-
toggle_selector.ES = EllipseSelector(ax, onselect)
2985-
fig.canvas.mpl_connect('key_press_event', toggle_selector)
2986-
plt.show()
2979+
Examples
2980+
--------
2981+
>>> import numpy as np
2982+
>>> import matplotlib.pyplot as plt
2983+
>>> from matplotlib.widgets import EllipseSelector
2984+
>>> def onselect(eclick, erelease):
2985+
... "eclick and erelease are matplotlib events at press and release."
2986+
... print(f'startposition: {eclick.xdata}, {eclick.ydata})
2987+
... print(f'endposition : {erelease.xdata}, {erelease.ydata})
2988+
... print('used button : ', eclick.button)
2989+
...
2990+
>>> def toggle_selector(event):
2991+
... print(' Key pressed.')
2992+
... if event.key in ['Q', 'q'] and toggle_selector.ES.active:
2993+
... print('EllipseSelector deactivated.')
2994+
... toggle_selector.RS.set_active(False)
2995+
... if event.key in ['A', 'a'] and not toggle_selector.ES.active:
2996+
... print('EllipseSelector activated.')
2997+
... toggle_selector.ES.set_active(True)
2998+
...
2999+
>>> x = np.arange(100.) / 99
3000+
>>> y = np.sin(x)
3001+
>>> fig, ax = plt.subplots()
3002+
>>> ax.plot(x, y)
3003+
>>> toggle_selector.ES = EllipseSelector(ax, onselect)
3004+
>>> fig.canvas.mpl_connect('key_press_event', toggle_selector)
3005+
>>> plt.show()
29873006
"""
3007+
__doc__ %= (_RECTANGLESELECTOR_PARAMETERS_DOCSTRING)
3008+
29883009
_shape_klass = Ellipse
29893010
draw_shape = _api.deprecate_privatize_attribute('3.5')
29903011

0 commit comments

Comments
 (0)