【问题标题】:Trying to make a gif in Python尝试在 Python 中制作 gif
【发布时间】:2020-01-31 01:27:57
【问题描述】:

我正在用傅里叶级数逼近函数,希望以一种简单的方式制作不同逼近的动画 gif。使用forplt.savefig 命令,我为 gif 生成了不同的帧,但我无法为它们设置动画。我们的想法是获得这样的东西

【问题讨论】:

    标签: python matplotlib animation


    【解决方案1】:

    这可以使用matplotlib.animation 来完成。

    编辑

    我将向您展示如何绘制 x 的偶数幂,例如 x^2x^4 等。看看下面的例子:

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    # Setting up the figure, the axis, and the plot element we want to animate
    fig = plt.figure()
    ax = plt.axes(xlim=(-10, 10), ylim=(-10, 1000))
    line, = ax.plot([], [], lw=2)
    
    # initialization method: plot the background of each frame
    def init():
        line.set_data([], [])
        return line,
    
    # animation method.  This method will be called sequentially
    pow_ = 2
    def animate(i):
        global pow_
        x = np.linspace(-10, 10, 1000)
        y = x**pow_
        pow_+=2
        line.set_data(x, y)
        return line,
    
    # call the animator.  blit=True means only re-draw the parts that have changed.
    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=10, interval=200, blit=True)
    
    # if you want to save this animation as an mp4 file, uncomment the line bellow
    # anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
    
    plt.show()
    

    【讨论】:

    • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    • @Selcuk 我已经编辑了我的答案并解释了一个例子,teşekkürler abi :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多