【发布时间】:2017-02-20 00:10:44
【问题描述】:
我想根据实时情节创建电影,我会不断更新情节中的数据。 由于原始代码太长且太复杂,我将原始数据替换为更简单的代码,以专注于保存电影的问题。
1.) 显示实时绘图工作正常
2.) 使用下面的代码保存电影(不显示实时情节)也可以正常工作
3.) !!!但是同时显示实时绘图和保存电影会产生一个电影,其中电影中的数字窗口正在缓慢向上移动!!!
这个问题可能是什么原因???
电影截图链接: https://i.stack.imgur.com/1xWcM.jpg
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as manimation
# we generate some synthetic data.
# we will make a list of X, Y lists.
# the original code use for generating data is too long and complex to show here
frames = []
frameNumber = 240 # we generate 240 different frames
for i in range(0, frameNumber):
xData = np.linspace(0, 2 * np.pi)
yData = np.sin(xData - i * 2.0 * np.pi / frameNumber)
frames.append([xData, yData])
print('frameNumber: ', i)
# now we put them together to make a movie! let's set up the movie writer
framerate = 60 # 24 frames per second
# FFMpegWriter = manimation.writers['avconv']
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Wave Data', artist='Isaac Newton', comment='')
writer = FFMpegWriter(fps=framerate, metadata=metadata)
# figure setup
fig,ax = plt.subplots()
firstFrameX, firstFrameY = frames[0]
l, = ax.plot(firstFrameX, firstFrameY, '-')
plt.ylabel(r'$x$ axis')
plt.xlabel(r'$y$ axis')
plt.xlim(0, 2 * np.pi)
plt.title(r'$\lambda = 2 \pi$')
# --- without displaying ---> comment next 2 lines
fig.show()
fig.canvas.draw()
# ------------------------------------------------
# let's write to the file!
with writer.saving(fig, "anim.mp4", 100):
for i in range(frameNumber):
x, y = frames[i]
l.set_data(x, y)
# --- without displaying ---> comment next 4 lines
ax.draw_artist(ax.patch)
ax.draw_artist(l)
fig.canvas.update()
fig.canvas.flush_events()
# ------------------------------------------------
writer.grab_frame()
【问题讨论】:
标签: python plot save real-time movie