繁体   English   中英

matplotlib 中的多行 animation

[英]Multiple lines animation in matplotlib

我想像这样在 matplolib 图形上设置线条动画:

线条动画

只有水平线让我感兴趣。

我制作了一个 function 动画,就像上面的 model 一样:

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)

现在的问题是我需要十个带有换行的动画。 我尝试了几件事,例如每行创建一个 function,创建 10 行,将 init function 放在“ani”中,但没有任何效果,因为我对动画的工作方式知之甚少。

这个 animation 的要点是为连续性错觉获得正确的间距。 我尽量保持你的结构不变。

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

Output:

在此处输入图像描述

暂无
暂无

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

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