【问题标题】:Python 3.6 - saving movie from real time plot - figure window in movie is moving upwardsPython 3.6 - 从实时情节中保存电影 - 电影中的图形窗口向上移动
【发布时间】: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


    【解决方案1】:

    我在 Python 2.7 中使用 matplotlib.animation 时遇到了类似的问题。

    对于第一次在新控制台中运行,我的电影会很好,但如果我再次运行脚本,生成的电影会向上移动,就像在您的屏幕截图中一样。

    重新启动控制台为我解决了这个问题,但不是很方便。

    通过在脚本末尾添加 plt.close(fig) 来关闭图,为我解决了问题。

    但是,我仍然不明白到底是什么原因造成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多