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

Skip to content

Backport PR #28039 on branch v3.9.x (Respect vertical_axis when rotating plot interactively) #28160

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

Merged
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
14 changes: 11 additions & 3 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,31 @@ 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')
Expand Down
Loading