You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/api/toolkits/mplot3d/view_angles.rst
+165Lines changed: 165 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -38,3 +38,168 @@ further documented in the `.mplot3d.axes3d.Axes3D.view_init` API.
38
38
39
39
.. plot:: gallery/mplot3d/view_planes_3d.py
40
40
:align: center
41
+
42
+
43
+
.. _toolkit_mouse-rotation:
44
+
45
+
Rotation with mouse
46
+
===================
47
+
48
+
3D plots can be reoriented by dragging the mouse.
49
+
There are various ways to accomplish this; the style of mouse rotation
50
+
can be specified by setting :rc:`axes3d.mouserotationstyle`, see
51
+
:doc:`/users/explain/customizing`.
52
+
53
+
Prior to v3.10, the 2D mouse position corresponded directly
54
+
to azimuth and elevation; this is also how it is done
55
+
in `MATLAB <https://www.mathworks.com/help/matlab/ref/view.html>`_.
56
+
To keep it this way, set ``mouserotationstyle: azel``.
57
+
This approach works fine for spherical coordinate plots, where the *z* axis is special;
58
+
however, it leads to a kind of 'gimbal lock' when looking down the *z* axis:
59
+
the plot reacts differently to mouse movement, dependent on the particular
60
+
orientation at hand. Also, 'roll' cannot be controlled.
61
+
62
+
As an alternative, there are various mouse rotation styles where the mouse
63
+
manipulates a virtual 'trackball'. In its simplest form (``mouserotationstyle: trackball``),
64
+
the trackball rotates around an in-plane axis perpendicular to the mouse motion
65
+
(it is as if there is a plate laying on the trackball; the plate itself is fixed
66
+
in orientation, but you can drag the plate with the mouse, thus rotating the ball).
67
+
This is more natural to work with than the ``azel`` style; however,
68
+
the plot cannot be easily rotated around the viewing direction - one has to
69
+
move the mouse in circles with a handedness opposite to the desired rotation,
70
+
counterintuitively.
71
+
72
+
A different variety of trackball rotates along the shortest arc on the virtual
73
+
sphere (``mouserotationstyle: sphere``). Rotating around the viewing direction
74
+
is straightforward with it: grab the ball near its edge instead of near the center.
75
+
76
+
Ken Shoemake's ARCBALL [Shoemake1992]_ is also available (``mouserotationstyle: Shoemake``);
77
+
it resembles the ``sphere`` style, but is free of hysteresis,
78
+
i.e., returning mouse to the original position
79
+
returns the figure to its original orientation; the rotation is independent
80
+
of the details of the path the mouse took, which could be desirable.
81
+
However, Shoemake's arcball rotates at twice the angular rate of the
82
+
mouse movement (it is quite noticeable, especially when adjusting roll),
83
+
and it lacks an obvious mechanical equivalent; arguably, the path-independent
84
+
rotation is not natural (however convenient), it could take some getting used to.
85
+
So it is a trade-off.
86
+
87
+
Henriksen et al. [Henriksen2002]_ provide an overview. In summary:
88
+
89
+
.. list-table::
90
+
:width: 100%
91
+
:widths: 30 20 20 20 20 35
92
+
93
+
* - Style
94
+
- traditional [1]_
95
+
- incl. roll [2]_
96
+
- uniform [3]_
97
+
- path independent [4]_
98
+
- mechanical counterpart [5]_
99
+
* - azel
100
+
- ✔️
101
+
- ❌
102
+
- ❌
103
+
- ✔️
104
+
- ✔️
105
+
* - trackball
106
+
- ❌
107
+
- ✓ [6]_
108
+
- ✔️
109
+
- ❌
110
+
- ✔️
111
+
* - sphere
112
+
- ❌
113
+
- ✔️
114
+
- ✔️
115
+
- ❌
116
+
- ✔️
117
+
* - arcball
118
+
- ❌
119
+
- ✔️
120
+
- ✔️
121
+
- ✔️
122
+
- ❌
123
+
124
+
125
+
.. [1] The way it was prior to v3.10; this is also MATLAB's style
126
+
.. [2] Mouse controls roll too (not only azimuth and elevation)
127
+
.. [3] Figure reacts the same way to mouse movements, regardless of orientation (no difference between 'poles' and 'equator')
128
+
.. [4] Returning mouse to original position returns figure to original orientation (rotation is independent of the details of the path the mouse took)
129
+
.. [5] The style has a corresponding natural implementation as a mechanical device
130
+
.. [6] While it is possible to control roll with the ``trackball`` style, this is not immediately obvious (it requires moving the mouse in large circles) and a bit counterintuitive (the resulting roll is in the opposite direction)
131
+
132
+
You can try out one of the various mouse rotation styles using:
133
+
134
+
.. code::
135
+
136
+
import matplotlib as mpl
137
+
mpl.rcParams['axes3d.mouserotationstyle'] = 'trackball' # 'azel', 'trackball', 'sphere', or 'arcball'
138
+
139
+
import numpy as np
140
+
import matplotlib.pyplot as plt
141
+
from matplotlib import cm
142
+
143
+
ax = plt.figure().add_subplot(projection='3d')
144
+
145
+
X = np.arange(-5, 5, 0.25)
146
+
Y = np.arange(-5, 5, 0.25)
147
+
X, Y = np.meshgrid(X, Y)
148
+
R = np.sqrt(X**2 + Y**2)
149
+
Z = np.sin(R)
150
+
151
+
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
152
+
linewidth=0, antialiased=False)
153
+
154
+
plt.show()
155
+
156
+
Alternatively, create a file ``matplotlibrc``, with contents::
157
+
158
+
axes3d.mouserotationstyle: trackball
159
+
160
+
(or any of the other styles, instead of ``trackball``), and then run any of
161
+
the :ref:`mplot3d-examples-index` examples.
162
+
163
+
The size of the virtual trackball, sphere, or arcball can be adjusted
164
+
by setting :rc:`axes3d.trackballsize`. This specifies how much
165
+
mouse motion is needed to obtain a given rotation angle (when near the center),
166
+
and it controls where the edge of the sphere or arcball is (how far from
167
+
the center, hence how close to the plot edge).
168
+
The size is specified in units of the Axes bounding box,
169
+
i.e., to make the arcball span the whole bounding box, set it to 1.
170
+
A size of about 2/3 appears to work reasonably well; this is the default.
171
+
172
+
Both arcballs (``mouserotationstyle: sphere`` and
173
+
``mouserotationstyle: arcball``) have a noticeable edge; the edge can be made
174
+
less abrupt by specifying a border width, :rc:`axes3d.trackballborder`.
175
+
This works somewhat like Gavin Bell's arcball, which was
176
+
originally written for OpenGL [Bell1988]_, and is used in Blender and Meshlab.
177
+
Bell's arcball extends the arcball's spherical control surface with a hyperbola;
178
+
the two are smoothly joined. However, the hyperbola extends all the way beyond
179
+
the edge of the plot. In the mplot3d sphere and arcball style, the border extends
180
+
to a radius ``trackballsize/2 + trackballborder``.
181
+
Beyond the border, the style works like the original: it controls roll only.
182
+
A border width of about 0.2 appears to work well; this is the default.
183
+
To obtain the original Shoemake's arcball with a sharp border,
184
+
set the border width to 0.
185
+
For an extended border similar to Bell's arcball, where the transition from
186
+
the arcball to the border occurs at 45°, set the border width to
187
+
:math:`\sqrt 2 \approx 1.414`.
188
+
The border is a circular arc, wrapped around the arcball sphere cylindrically
189
+
(like a doughnut), joined smoothly to the sphere, much like Bell's hyperbola.
190
+
191
+
192
+
.. [Shoemake1992] Ken Shoemake, "ARCBALL: A user interface for specifying
193
+
three-dimensional rotation using a mouse", in Proceedings of Graphics
194
+
Interface '92, 1992, pp. 151-156, https://doi.org/10.20380/GI1992.18
195
+
196
+
.. [Bell1988] Gavin Bell, in the examples included with the GLUT (OpenGL
0 commit comments