Skip to content

Fix rotation selector for aspect ratio of the axes values different from 1 #21886

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,29 @@ def onselect(epress, erelease):
tool._selection_artist.rotation_point = 'unvalid_value'


def test_rectangle_rotate_aspect_ratio():
_, ax = plt.subplots()
ax.plot([1, 2, 3], [10, 20, 30])

def onselect(epress, erelease):
pass

tool = widgets.RectangleSelector(ax, onselect=onselect, interactive=True)

# Draw rectangle
do_event(tool, 'press', xdata=1, ydata=10)
do_event(tool, 'onmove', xdata=1.5, ydata=14)
do_event(tool, 'release', xdata=1.5, ydata=14)
assert tool.extents == (1.0, 1.5, 10.0, 14.0)

# Rotate clockwise using bottom-right corner
do_event(tool, 'on_key_press', key='r')
do_event(tool, 'press', xdata=1.5, ydata=10)
do_event(tool, 'onmove', xdata=1.4, ydata=8.7)
do_event(tool, 'release', xdata=1.4, ydata=8.7)
assert_allclose(tool.rotation, -27.8, rtol=0.1)


def test_rectange_add_remove_set():
ax = get_ax()

Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3087,8 +3087,9 @@ def _onmove(self, event):
a = np.array([eventpress.xdata, eventpress.ydata])
b = np.array(self.center)
c = np.array([event.xdata, event.ydata])
angle = (np.arctan2(c[1]-b[1], c[0]-b[0]) -
np.arctan2(a[1]-b[1], a[0]-b[0]))
ax_corr = self.ax._get_aspect_ratio()
angle = (np.arctan2((c[1]-b[1]) * ax_corr, c[0]-b[0]) -
np.arctan2((a[1]-b[1]) * ax_corr, a[0]-b[0]))
self.rotation = np.rad2deg(self._rotation_on_press + angle)

# resize an existing shape
Expand Down