我正试图用MatPlotLib在Python中创建一个动画。但是,我不知道我下面的代码出了什么问题。它正在生成一个空白图像。
from matplotlib import pyplot as plt
import matplotlib.animation as animation
domain = [0., 1., 2.]
images = [[0., 0., 0.],
[1., 1., 1.],
[2., 2., 2.]]
figure = plt.figure()
axes = plt.axes()
graph = axes.plot([])
def set_frame(image):
graph.set_data(domain, image)
return graph
animation.FuncAnimation(figure, set_frame, frames=images)
plt.ylim(0., 2.)
plt.show()
-
您需要将动画分配给变量以防止其被删除
-
graph
是检索元素所需的列表
from matplotlib import pyplot as plt
import matplotlib.animation as animation
domain = [0., 1., 2.]
images = [[0., 0., 0.],
[1., 1., 1.],
[2., 2., 2.]]
figure = plt.figure()
axes = plt.axes()
graph = axes.plot([])
def set_frame(image):
graph[0].set_data(domain, image)
return graph
_ = animation.FuncAnimation(figure, set_frame, frames=images)
plt.ylim(0., 2.)
plt.show()