web-dev-qa-db-fra.com

Rotation interactive d'un tracé 3D dans python - matplotlib - Jupyter Notebook

Je me demandais comment il était possible de faire pivoter interactivement un tracé 3D comme décrit dans la vidéo this (si vous décidez d'en haut ou en dessous ou de droite ou de gauche). Je peux générer un tracé 3D dans spyder ou dans un cahier jupyter mais après cela, il reste statique et je ne peux pas interagir avec lui et faire pivoter/changer l'angle du point de vue.

Voici le code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

scale = 8
# Make data.
X = np.arange(-scale, scale, 0.25)
Y = np.arange(-scale, scale, 0.25)
X, Y = np.meshgrid(X, Y)
Z = X**2 + Y**2

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                   linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(0, 100)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# rotate the axes and update
for angle in range(0, 360):
   ax.view_init(30, 40)

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

Merci beaucoup d'avance!

5
ecjb

Ok j'ai trouvé la réponse sur un autre post:

Comment puis-je ouvrir la fenêtre interactive Matplotlib dans le cahier IPython? .

la ligne suivante %matplotlib qt doit être écrit au début du script + vous devez redémarrer le cahier jupyter et ça marche

Merci beaucoup pour votre aide!

8
ecjb