【问题标题】:FuncAnimation update function not called未调用 FuncAnimation 更新函数
【发布时间】:2018-10-12 13:44:03
【问题描述】:

我不太了解如何创建用于动画数据的类。这是要点:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x = np.arange(100).reshape((100, 1))
y = np.random.randn(100, 1)
xy = np.hstack((x, y))


class PlotData:
    def __init__(self):
        fig, ax = plt.subplots()
        fig.set_size_inches((11, 9))
        self.fig = fig
        self.ax = ax
        self.ln0, = ax.plot([], [])

    def init(self):
        self.ln0.set_data([], [])
        return(self.ln0, )

    def update(self, frame_no):
        data = xy[0:frame_no + 1]
        self.ln0.set_data(data[:, 0], data[:, 1])
        return(self.ln0, )


if __name__ == '__main__':
    my_plot = PlotData()
    anim = animation.FuncAnimation(my_plot.fig, my_plot.update,
                                   init_func=my_plot.init, blit=True,
                                   frames=99, interval=50)
    plt.show()

这只会产生 init 方法输出,但不会产生更新,因此最终会产生一个没有动画的空白图。怎么回事?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    对我来说,您的代码运行良好。唯一的问题是大多数数据都超出了绘图限制。如果你像这样调整你的情节限制:

    class PlotData:
        def __init__(self):
            fig, ax = plt.subplots(figsize = (11,9))
            self.fig = fig
            self.ax = ax
            self.ax.set_xlim([0,100])
            self.ax.set_ylim([-3,3])
            self.ln0, = ax.plot([], [])
    

    线条的动画效果很好。如果您希望自动调整 x 和 y 限制,请参阅this question 了解如何操作。但是,如果我没记错的话,这只适用于blit=False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-19
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      相关资源
      最近更新 更多