Repository: matplotlib/matplotlib

Issue:
[Bug]: vertical_axis not respected when rotating plots interactively
### Bug summary

 When setting `ax.view_init(vertical_axis="x")` the plot is initialized as intended but once rotating it a little the plot and `ax._vertical_axis` is reset to the default value.

### Code for reproduction

```Python
x = np.array([0, 1, 2, 4])
y = np.array([5, 10])
z = np.array([100, 150, 200])
X, Y, Z = np.meshgrid(*[x, y, z], indexing="ij")
c = np.arange(0, X.size)

plt.figure()
for j, (a, e) in enumerate([(0, 0), (-60, 30)]):
    for i, vert_a in enumerate(["z", "y", "x"]):
        ax = plt.subplot(2, 3, j * 3 + 1 + i, projection="3d")
        pc = ax.scatter(X, Y, Z, c=c)
        # ax.view_init(vertical_axis=vert_a)
        # ax.view_init(azim=0, elev=0, vertical_axis=vert_a)
        # ax.view_init(azim=-60, elev=30, vertical_axis=vert_a)
        ax.view_init(azim=a, elev=e, vertical_axis=vert_a)
        ax.set_title(f"azim={a}, elev={e}, vertical_axis='{vert_a}'")
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.set_zlabel("z")
```


### Actual outcome

Inital view:
![image](https://github.com/matplotlib/matplotlib/assets/14371165/62171edf-dc47-4588-abff-d7ebd12c992b)

After a small rotation on the **bottom right** plot:
![image](https://github.com/matplotlib/matplotlib/assets/14371165/f37d9609-e4c8-461d-b373-ed9e27c42d35)


### Expected outcome

`ax._vertical_axis` should stay the same when rotating plots. This worked correctly in earlier matplotlib version. 

### Additional information

_No response_

### Operating system

Windows 10

### Matplotlib Version

3.8.3

### Matplotlib Backend

Qt5Agg

### Python version

3.12

### Jupyter version

_No response_

### Installation

conda


Discussion:

Patch:
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


Test patch:
diff --git a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
index 7662509dd9cf..731b0413bf65 100644
--- a/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
+++ b/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
@@ -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')