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

Skip to content

Allow Rectangle and Ellipse selectors to be rotated #19864

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

Closed
wants to merge 6 commits into from
Closed
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/selector_rotate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Rotating selectors
------------------

The `~matplotlib.widgets.RectangleSelector` and
`~matplotlib.widgets.EllipseSelector` can now be rotated by pressing the 'r'
key, and dragging one of their control points. This is currently only
implemented for equal-aspect axes. The rotation is done about the
first vertex placed when drawing the selector.
69 changes: 69 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,75 @@ def onselect(epress, erelease):
tool._corner_handles.artist.get_markeredgecolor(), 'b')


@pytest.mark.parametrize('selector_class', [widgets.RectangleSelector,
widgets.EllipseSelector])
def test_rectangle_rotate(selector_class):
ax = get_ax()

def onselect(epress, erelease):
pass

tool = selector_class(ax, onselect=onselect, interactive=True)
# Draw rectangle
do_event(tool, 'press', xdata=100, ydata=100)
do_event(tool, 'onmove', xdata=130, ydata=140)
do_event(tool, 'release', xdata=130, ydata=140)
assert tool.extents == (100, 130, 100, 140)

# Rotate anticlockwise using top-right corner
do_event(tool, 'on_key_press', key='r')
do_event(tool, 'press', xdata=130, ydata=140)
do_event(tool, 'onmove', xdata=100, ydata=150)
do_event(tool, 'release', xdata=100, ydata=200)
do_event(tool, 'on_key_release', key='r')
# Extents shouldn't change (as shape of rectangle hasn't changed)
assert tool.extents == (100, 130, 100, 140)
# Corners should move
# The third corner is at (100, 150), as the diagonal of the rectangle has
# length 50 (it is a 3,4,5 scaled right angled triangle)
assert tool.corners == ((100, 124, 100, 76), (100, 118, 150, 132))

# Scale using top-right corner
do_event(tool, 'press', xdata=100, ydata=150)
do_event(tool, 'onmove', xdata=100, ydata=125)
do_event(tool, 'release', xdata=100, ydata=125)
assert tool.extents == (100, 115, 100, 120)
# The bottom-left corner doesn't move, the top-right moves to (100, 125)
assert tool.corners == ((100, 112, 100, 88), (100, 109, 125, 116))


@pytest.mark.parametrize('selector_class', [widgets.RectangleSelector,
widgets.EllipseSelector])
def test_rotate_warning(selector_class):
# Check that a warning is raised when trying to rotate on a
# non-equal aspect Axes.
ax = get_ax()

def onselect(epress, erelease):
pass

tool = selector_class(ax, onselect=onselect, interactive=True)
ax.set_aspect(2)

# Draw rectangle
do_event(tool, 'press', xdata=100, ydata=100)
do_event(tool, 'onmove', xdata=130, ydata=140)
do_event(tool, 'release', xdata=130, ydata=140)
old_corners = tool.corners

# Try to rotate
do_event(tool, 'on_key_press', key='r')
do_event(tool, 'press', xdata=130, ydata=140)
# Check warning is raised
with pytest.warns(UserWarning, match='Rotation is only implemented for '
'equal-aspect Axes.'):
do_event(tool, 'onmove', xdata=100, ydata=150)
do_event(tool, 'release', xdata=100, ydata=200)
do_event(tool, 'on_key_release', key='r')
# Check that corners haven't moved
assert tool.corners == old_corners


def check_span(*args, **kwargs):
ax = get_ax()

Expand Down
Loading