Open
Description
As of 2.0, Axes3D
(from mplot3d
) inherits annotate
from the regular 2D Axes
class. As a consequence, it takes 2D coordinates as argument (i.e., the projection done internally by mplot3d before drawing), and annotations do not follow rotation or zooming of the 3D axes.
from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
pathcoll = ax.scatter(range(10), range(10), range(10))
# ax.annotate("foo", (0, 0, 0)) raises.
ax.annotate("foo", (0, 0))
# Need to draw so that pathcoll.get_offsets() is updated to the 2D projection
# of the coordinates.
fig.canvas.draw()
ax.annotate("bar", pathcoll.get_offsets()[5])
plt.show()
Note that "bar" is drawn at the correct position, which happens to be close to (0, 0) in the 2D projection used by mplot3d, so "foo" ends up close to it either.
Funnily enough, it means that mplcursors
"accidentally" works for 3D plots -- but the annotations do not follow rotation or zooming (that's how I found the issue)...