繁体   English   中英

未调用FuncAnimation更新功能

[英]FuncAnimation update function not called

我不太了解如何创建动画数据类。 要点如下:

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方法的输出,而不会产生更新,因此最终导致没有动画的空白图。 到底是怎么回事?

对我来说,您的代码工作得很好。 唯一的问题是,大多数数据超出了绘图限制。 如果您像这样调整地块限制:

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极限,请参阅此问题以了解如何进行。 但是,如果我没记错的话,这只有在blit=False才能正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM