Skip to content

Fix AxesWidgets on inset_axes that are outside their parent. #29966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,27 +623,35 @@ def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
('horizontal', False, dict(interactive=True)),
])
def test_span_selector(ax, orientation, onmove_callback, kwargs):
onselect = mock.Mock(spec=noop, return_value=None)
onmove = mock.Mock(spec=noop, return_value=None)
if onmove_callback:
kwargs['onmove_callback'] = onmove

# While at it, also test that span selectors work in the presence of twin axes on
# top of the axes that contain the selector. Note that we need to unforce the axes
# aspect here, otherwise the twin axes forces the original axes' limits (to respect
# aspect=1) which makes some of the values below go out of bounds.
# Also test that span selectors work in the presence of twin axes or for
# outside-inset axes on top of the axes that contain the selector. Note
# that we need to unforce the axes aspect here, otherwise the twin axes
# forces the original axes' limits (to respect aspect=1) which makes some
# of the values below go out of bounds.
ax.set_aspect("auto")
tax = ax.twinx()

tool = widgets.SpanSelector(ax, onselect, orientation, **kwargs)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
# move outside of axis
do_event(tool, 'onmove', xdata=199, ydata=199, button=1)
do_event(tool, 'release', xdata=250, ydata=250, button=1)

onselect.assert_called_once_with(100, 199)
if onmove_callback:
onmove.assert_called_once_with(100, 199)
ax.twinx()
child = ax.inset_axes([0, 1, 1, 1], xlim=(0, 200), ylim=(0, 200))

for target in [ax, child]:
selected = []
def onselect(*args): selected.append(args)
moved = []
def onmove(*args): moved.append(args)
if onmove_callback:
kwargs['onmove_callback'] = onmove

tool = widgets.SpanSelector(target, onselect, orientation, **kwargs)
x, y = target.transData.transform((100, 100))
MouseEvent('button_press_event', ax.figure.canvas, x, y, button=1)._process()
# move outside of axis
x, y = target.transData.transform((199, 199))
MouseEvent('motion_notify_event', ax.figure.canvas, x, y, button=1)._process()
do_event(tool, 'release', xdata=250, ydata=250, button=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why leave only one do_event here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to just do a mass replace of do_event by _from_ax_coords in #29993, and if anything, leaving the do_event there would be useful as a pinned note to remember changing that test as well.


# tol is set by pixel size (~100 pixels & span of 200 data units)
assert_allclose(selected, [(100, 199)], atol=.5)
if onmove_callback:
assert_allclose(moved, [(100, 199)], atol=.5)


@pytest.mark.parametrize('interactive', [True, False])
Expand Down
Loading
Loading