在 python 中交互旋转 3D 图 - matplotlib - Jupyter Notebook
Rotate interactively a 3D plot in python - matplotlib - Jupyter Notebook
我想知道如何按照 this 视频中的描述交互式地旋转 3D 图(如果您决定从上方或下方或从右侧或左侧旋转)。我可以在 spyder 或 jupyter Notebook 中生成 3D 图,但之后它保持静态,我无法与其交互,rotate/change 视角角度。
代码如下:
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()
非常感谢您!
好的,我在另一个 post 上找到了答案:
How can I open the interactive Matplotlib window in IPython notebook?.
必须在脚本开头写下一行%matplotlib qt
+ 你必须重新启动 jupyter notebook 才能正常工作
非常感谢你们的帮助!
我想知道如何按照 this 视频中的描述交互式地旋转 3D 图(如果您决定从上方或下方或从右侧或左侧旋转)。我可以在 spyder 或 jupyter Notebook 中生成 3D 图,但之后它保持静态,我无法与其交互,rotate/change 视角角度。
代码如下:
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()
非常感谢您!
好的,我在另一个 post 上找到了答案:
How can I open the interactive Matplotlib window in IPython notebook?.
必须在脚本开头写下一行%matplotlib qt
+ 你必须重新启动 jupyter notebook 才能正常工作
非常感谢你们的帮助!