我正在研究一个脚本,它在一系列数据点上沿x轴移动一条垂直线。动画与import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time
# Simulated background data
x = np.linspace(0,61,62)
y = np.linspace(0,6,62)
# Set up the figure, the axis, and the plot element we want to animate
max_height = 6 # max height of y-axis
n_pts = 61 # max length of x-axis
# Original location for progress line
y1 = [0, max_height]
x1 = [0, 0]
fig = plt.figure() # Initialize figure
#ax = fig.add_subplot(111) # Intialize axes
ax = plt.axes(xlim=(0, n_pts), ylim=(0, max_height)) # Set axes limits
line, = ax.plot([], [], lw=2) # Initialize line
# draw the data to the 'background'
line1, = ax.plot(x, y, color='black')
# initialization function: plot the background of each frame
def init():
line.set_data(x1, y1)
return line,
starttime=time.time()
mytimer=0
mytimer_ref=0
# animation function. This is called sequentially
def animate(i):
t = time.time() - starttime
mytimer = t + mytimer_ref
x1 = [mytimer,mytimer]
line.set_data(x1, y1)
return line,
# call the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=61, interval=1000)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
writer = animation.writers['ffmpeg'](fps=1)
anim.save('demo.mp4',writer=writer,dpi=dpi)
plt.show()
一起正常,但我输出电影文件时遇到问题。请注意我是Python的新手,虽然我已经玩了一两年了。该脚本是通过将this tutorial中的第一个脚本与this previous stack overflow question的答案中显示的脚本相结合而创建的。该线最终将在静态数据线图上移动,我在这里将其描述为对角线。
电影文件输出为具有正确的时间长度(1分钟,10秒),但该线应该从最左边移动到最右边,每秒1点,只移动几个像素输出视频。
非常感谢您提供解决此问题的任何帮助。
编辑:我在Ubuntu 14.04上运行Python 2.7.6。
这是我可重现的代码:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from time import time
from scipy.interpolate import interp1d
max_height = 6 # max height of y-axis
n_pts = 62 # max length of x-axis
x = np.linspace(0,61,62)
y = np.linspace(0,6,62)
# New figure with white background
fig = plt.figure(facecolor = 'white')
ax = plt.axes(aspect=1) # Set axes limits
# Original location for progress line
y1 = [0, max_height]
x1 = [0, 0]
# initialize line
plt.plot(x, y) #plot background
line, = ax.plot([], [], lw=2)
def update(frame):
x1 = frame
line.set_data(x1, y1)
# Return the modified line
return line,
anim = animation.FuncAnimation(fig, update, interval=1000, blit=True)
anim.save('line.mp4', writer='ffmpeg')
plt.show()
编辑以下脚本会创建影片并将其另存为mp4。现在的问题是虽然有61帧动画,但我不能让电影停在那里。每次进行100帧。我知道这个问题现在有点老了,但是非常感谢任何帮助!
我试图手动设置x轴,这限制了屏幕上显示的内容,但动画继续超出显示的轴。
auto indices = hana::to<hana::tuple_tag>(hana::range_c<std::size_t, 0, sizeof...(T)>);
auto types = hana::make_tuple(std::forward<T>(args)...);
hana::for_each(hana::zip(indices, types), hana::fuse([](auto i, auto&& x) {
// ...
}));
答案 0 :(得分:1)
对于您编辑的代码,请在调用animation.FuncAnimation
中包含另外两个参数:
frames=25
(25
选择为例) - 再次设置最大帧数repeat=False
- 不要在最大帧数后重复动画组合,你得到这个命令:
anim = animation.FuncAnimation(fig, update, frames=25,
interval=1000, blit=True, repeat=False)
这导致动画逐步超过25帧(在您的情况下,在一秒间隔内将垂直线从0
移动到24
) - 然后停在那里。