From d324f1c119f7e3a32f1961e5500b80b2af9febba Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Wed, 8 Dec 2021 22:28:18 +0000 Subject: [PATCH] Fix rotation for aspect ratio of the axes values different from 1 --- lib/matplotlib/tests/test_widgets.py | 23 +++++++++++++++++++++++ lib/matplotlib/widgets.py | 5 +++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_widgets.py b/lib/matplotlib/tests/test_widgets.py index 1986cd23f160..7220e269060d 100644 --- a/lib/matplotlib/tests/test_widgets.py +++ b/lib/matplotlib/tests/test_widgets.py @@ -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() diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 9c9fad1a20d5..69b33477b120 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -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