Closed
Description
Summary
If I remember correctly, at some point, @anntzer suggested to deprecate onselect
argument for selectors. Now, that I understand better what he meant at the time, I think it makes a lot of sense, because the behaviour of the "builtin" selector callback is contentious and inconsistent between selectors.
Proposed fix
The alternative would be something along the lines of:
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
import functools
_, ax = plt.subplots()
ax.plot([1, 2, 3])
def dummy_callback(*args):
pass
def onselect_callback(event, selector):
print('## onselect callback ##')
if not selector.ignore(event):
print(' ', selector.extents)
return
print(' event ignored')
def onmove_callback(event, selector):
print('## onmove callback ##')
if not selector.ignore(event) and selector._eventpress:
print(' ', selector.extents)
return
print(' event ignored')
tool = RectangleSelector(ax, dummy_callback, interactive=True,
drag_from_anywhere=True,)
tool.extents = (1.2, 2.0, 1.0, 1.6)
# alternative to onselect
rectangle_onselect_callback = functools.partial(onselect_callback, selector=tool)
tool.connect_event('button_release_event', rectangle_onselect_callback)
# alternative to onmove_callback (SpanSelector only)
rectangle_onmove_callback = functools.partial(onmove_callback, selector=tool)
tool.connect_event('motion_notify_event', rectangle_onmove_callback)
In my opinion, the reasons to deprecate are:
- provide consistent API between selectors
- leave the user define what is the expected behaviour - anyway, I don't think the current API is obvious to use
- fix Widgets: RectangleSelector in Mpl 2.1.0 does not behave as described #9608 (see Fix for #9608: RectangleSelector now reports mouse button press and release events correctly #20611 for more discussion)
- simplify maintenance
Proposed fix:
- Deprecate the
onselect
/onmove
arguments of the selectors, as per example above - improve the API to ignore events (the example above uses a private API to reproduce the
onmove_callback
of theSpanSelector
) - update example, improve documentation on the use of callback registry.