【问题标题】:Making an Animation with Matplotlib使用 Matplotlib 制作动画
【发布时间】:2022-01-22 08:54:45
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

    标签: python matplotlib graphics


    【解决方案1】:
    1. 您需要将动画分配给变量以防止其被删除

    2. 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()
    

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 2017-06-21
      • 2021-04-03
      • 2020-08-25
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多