From d0e06320859f872517fc1eb5ea5d6d1ad87d4f42 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 7 Apr 2024 15:59:57 +0200 Subject: [PATCH 1/5] Add rotation test with vertical_axis --- lib/mpl_toolkits/mplot3d/tests/test_axes3d.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py index 7662509dd9cf..fdbc0eb9fa45 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py @@ -2250,6 +2250,32 @@ def test_view_init_vertical_axis( np.testing.assert_array_equal(tickdir_expected, tickdir_actual) +@pytest.mark.parametrize("vertical_axis", ["x", "y", "z"]) +def test_on_move_vertical_axis(vertical_axis: str) -> None: + """ + Test vertical axis is respected when rotating the plot interactively. + """ + ax = plt.subplot(1, 1, 1, projection="3d") + ax.view_init(elev=0, azim=0, roll=0, vertical_axis=vertical_axis) + ax.figure.canvas.draw() + + proj_before = ax.get_proj() + event_click = mock_event(ax, button=MouseButton.LEFT, xdata=0, ydata=1) + ax._button_press(event_click) + + event_move = mock_event(ax, button=MouseButton.LEFT, xdata=0.5, ydata=0.8) + ax._on_move(event_move) + + assert ax._axis_names.index(vertical_axis) == ax._vertical_axis + + # Make sure plot has actually moved: + proj_after = ax.get_proj() + np.testing.assert_raises( + AssertionError, np.testing.assert_allclose, proj_before, proj_after + ) + + + @image_comparison(baseline_images=['arc_pathpatch.png'], remove_text=True, style='mpl20') From abba3a6f8e9dbe2b798f43333cae295242855d90 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 7 Apr 2024 16:18:08 +0200 Subject: [PATCH 2/5] Update test_axes3d.py --- lib/mpl_toolkits/mplot3d/tests/test_axes3d.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py index fdbc0eb9fa45..731b0413bf65 100644 --- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py +++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py @@ -2275,7 +2275,6 @@ def test_on_move_vertical_axis(vertical_axis: str) -> None: ) - @image_comparison(baseline_images=['arc_pathpatch.png'], remove_text=True, style='mpl20') From d0b03d2927979ff766a2044023b0111f4436b1d8 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 7 Apr 2024 16:19:07 +0200 Subject: [PATCH 3/5] Add fix, _on_move used vew_init without all arguments --- lib/mpl_toolkits/mplot3d/axes3d.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index 9ca5692c40ab..d0f5c8d2b23b 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -1147,7 +1147,8 @@ def view_init(self, elev=None, azim=None, roll=None, vertical_axis="z", if roll is None: roll = self.initial_roll vertical_axis = _api.check_getitem( - dict(x=0, y=1, z=2), vertical_axis=vertical_axis + {name: idx for idx, name in enumerate(self._axis_names)}, + vertical_axis=vertical_axis, ) if share: @@ -1318,7 +1319,7 @@ def shareview(self, other): raise ValueError("view angles are already shared") self._shared_axes["view"].join(self, other) self._shareview = other - vertical_axis = {0: "x", 1: "y", 2: "z"}[other._vertical_axis] + vertical_axis = self._axis_names[other._vertical_axis] self.view_init(elev=other.elev, azim=other.azim, roll=other.roll, vertical_axis=vertical_axis, share=True) @@ -1523,7 +1524,14 @@ def _on_move(self, event): dazim = -(dy/h)*180*np.sin(roll) - (dx/w)*180*np.cos(roll) elev = self.elev + delev azim = self.azim + dazim - self.view_init(elev=elev, azim=azim, roll=roll, share=True) + vertical_axis = self._axis_names[self._vertical_axis] + self.view_init( + elev=elev, + azim=azim, + roll=roll, + vertical_axis=vertical_axis, + share=True, + ) self.stale = True # Pan From f4505a5baa2f2da6162095e4b9e68a4d694432d5 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 7 Apr 2024 17:04:34 +0200 Subject: [PATCH 4/5] Add whats new --- .../respect_vertical_axis_when_rotating_interactively.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst diff --git a/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst b/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst new file mode 100644 index 000000000000..c27554733e39 --- /dev/null +++ b/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst @@ -0,0 +1,5 @@ +Respect vertical axis when rotating 3D plots +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Fixed a bug where the vertical_axis in ax.view_init was reset to the default +value when rotating the plot interactively. From c16804f5fb192c4eea66bfe5b6e682cdf12e5c45 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 17 Apr 2024 19:46:10 +0200 Subject: [PATCH 5/5] Delete respect_vertical_axis_when_rotating_interactively.rst --- .../respect_vertical_axis_when_rotating_interactively.rst | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst diff --git a/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst b/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst deleted file mode 100644 index c27554733e39..000000000000 --- a/doc/users/next_whats_new/respect_vertical_axis_when_rotating_interactively.rst +++ /dev/null @@ -1,5 +0,0 @@ -Respect vertical axis when rotating 3D plots -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Fixed a bug where the vertical_axis in ax.view_init was reset to the default -value when rotating the plot interactively.