在python中交互式旋转3D图-Matplotlib-Jupyter Notebook

ecj*_*cjb 5 python 3d interactive matplotlib jupyter-notebook

我想知道如何像视频所述交互式地旋转3D图(如果您从上方或下方或从右或左进行选择)。我可以在spyder或jupyter Notebook中生成3D图,但此后它保持静态,无法与之交互并旋转/更改视点角度。

这是代码:

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()
Run Code Online (Sandbox Code Playgroud)

提前非常感谢您!

ecj*_*cjb 6

好吧,我在另一篇文章中找到了答案:

如何在IPython Notebook中打开交互式Matplotlib窗口?

%matplotlib qt必须在脚本的开头编写以下行+您必须重新启动jupyter笔记本,它才能正常工作

非常感谢您的帮助!


小智 2

您还提到了间谍。对于 Spyder 用户:工具 --> 首选项 --> IPython 控制台,选择“图形”选项卡 --> 在图形后端中,选择自动。(基于 Windows 上的 Spyder 5.xx)。