交互式旋转3D图表

18
我想知道如何交互式地旋转一个三维图形,就像this视频中描述的那样(无论是从上方、下方、右侧还是左侧决定视角)。我可以在Spyder或Jupyter Notebook中生成一个三维图形,但之后它仍然是静态的,我无法与之交互并旋转/改变视点的角度。
这是代码:
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()

1
你需要使用一个交互式的后端。默认情况下,Jupyter以及Spyder中的IPython控制台使用内联后端,它基本上显示绘图的png图像 - png图像不是交互式的。 - ImportanceOfBeingErnest
非常感谢@ImportanceOfBeingErnest。您是否有关于如何确切地做到这一点的材料(教程,网站)? - ecjb
不了解matplotlib,但对于交互式绘图,我可以推荐使用pyqtgraph,它使用Qt库进行绘图,速度非常快。 - Alfe
请搜索类似“互动的matplotlib jupyter”之类的内容。 - ImportanceOfBeingErnest
@ImportanceOfBeingErnest哦,可以的。请看https://youtu.be/vSbyDoXqAJI?t=4m32s。 - Alfe
2个回答

23

不需要重新启动,再次导入matplotlib可能会起作用。 - user202729

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接