Matplotlib 动画不渲染

Matplotlib Animation not rendering

我正在尝试渲染旋转 3d 图形的动画,但动画不起作用,即使我尝试使用此处给出的代码也是如此:https://matplotlib.org/3.5.0/gallery/mplot3d/rotate_axes3d_sgskip.html

我得到的输出是一张静态图像

一遍又一遍地重复 multipe 次。

我正在使用 anaconda、jupyter notebooks,还尝试使用 google colab...出了什么问题,我该如何解决?非常感谢!

由于您使用的是 Jupyter Notebook,因此您需要安装 ipympl 并在笔记本顶部的单元格中执行以下命令:%matplotlib widget。这将启用一个交互式框架,它将在输出单元格中包装 matplotlib 图。

那么,你需要使用matplotlib的FuncAnimation:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

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

# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

def animate(angle):
    ax.view_init(30, angle)

ani = FuncAnimation(fig, animate, frames=range(360))

在 Jupyter notebook 的单元格中添加 %matplotlib notebook 作为交互式后端。 执行下面的单元格,它应该可以工作

%matplotlib notebook
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

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

# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

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

我 运行 之前遇到过类似的问题,在给定代码中将“%matplotlib inline”替换为“%matplotlib notebook”为我解决了。