【问题标题】:Multiple lines animation in matplotlibmatplotlib 中的多行动画
【发布时间】:2021-01-16 22:11:29
【问题描述】:

我想像这样在 matplolib 图形上制作线条动画:

我只对水平线感兴趣。

我制作了一个函数,可以像上面的模型那样对一条线进行动画处理:

def animate(i):
    line.set_ydata(0 - np.exp(i/20))
    return line,

line, = ax.plot((-100, 100), (0,0), 'r', lw=1)

ani = animation.FuncAnimation(fig, animate, frames=np.linspace(0, 78, 79), blit=True, interval=20,
repeat=True)

现在的问题是我需要十个带有换行的动画。 我尝试了几件事,例如每行创建一个函数,创建 10 行,在“ani”中放置一个 init 函数,但没有任何效果,因为我对动画的工作原理了解不够。

【问题讨论】:

    标签: python matplotlib animation


    【解决方案1】:

    这个动画的重点是让间距正确,以产生连续性的错觉。我尽量保持你的结构不变。

    from matplotlib import pyplot as plt
    import numpy as np
    import matplotlib.animation as anim
    
    #hey, it's in CinemaScope   
    fig, ax = plt.subplots(figsize=(11.75, 5))
    xrange = (-100, 100)
    numb_of_lines = 8
    frames = 30
    scal_fac = 50
    #define the y-range, so that the last line disappears outside the frame
    #also set the upper range in a ratio that is used in photography to evoke a pleasant image composition 
    yrange = (-0.99 * np.exp(frames * numb_of_lines/scal_fac), np.exp(frames * numb_of_lines /scal_fac)/2)
    ax.set_xlim(xrange)
    ax.set_ylim(yrange)
    ax.set_xticks([])
    ax.set_yticks([])
    
    #set unchanging horizon line to improve the illusion of continuity
    ax.plot(xrange, (-1,-1), 'r', lw=1)
    
    #set initial lines
    line_coll = []
    for j in range(numb_of_lines):
        line,  = ax.plot(xrange, (-np.exp(frames * j/scal_fac),-np.exp(frames * j/scal_fac)), 'r', lw=1)
        line_coll.append(line)    
    
    def animate(i):   
        for j in range(numb_of_lines):
            line_coll[j].set_ydata(-np.exp((i + j * frames)/scal_fac)) 
        return line_coll  
        
    ani = anim.FuncAnimation(fig, animate, frames=frames, blit=True, repeat=True, interval=20)
    plt.show()
    

    输出:

    【讨论】:

    • 它的作品!谢谢 !我确认我无法做到这一点......
    • 谢谢。我的印象是指数函数会产生旋转的错觉,而您上面的示例看起来像是在平面上移动。但是,当您添加更多暗示透视的视觉元素或修改速度/行数/帧数时,这种情况可能会发生变化。我敢打赌,这已经在心理学研究中得到了很好的检验。
    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2015-08-16
    • 2021-04-08
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多