该动画的主要目的是使间距正确,以产生连续性的错觉。我尽量保持你的结构不变。
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()
输出:
