高效的开/关开关,用于在Python

时间:2018-06-11 09:34:17

标签: python animation plot

我有一个Python程序,我在其中使用Matplotlib同时构建两个动画并将它们保存到外部mp4电影文件中。

我想构建

的开关

____ 1)打开/关闭绘图和保存

____ 2)仅打开/关闭保存

由于涉及许多绘图命令需要花费大量计算时间,因此跳过所有绘图命令非常重要,而不是仅仅跳过plt.draw()命令。

最好/最恐怖的方法是什么?

最低工作示例:

from initiate_axes import initiate_axes
import matplotlib.pyplot as plt
import matplotlib.animation as manimation

# FFMPEG writer for recording the Matplotlib output.
FFMPEGWriter = manimation.writers['ffmpeg']
writer = {'figure_0': FFMPEGWriter(fps=1 / time_increment),
          'figure_1': FFMPEGWriter(fps=1 / time_increment)}

# Figure and axes settings.
figures, axes = initiate_axes()

with writer['figure_0'].saving(figures['figure_0'], 'figure_0.mp4'), \
     writer['figure_1'].saving(figures['figure_1'], 'figure_1.mp4'):

    for iteration in range(1e3):

        # Nonplotting stuff which has to be done.
        # ...

        # Plotting.
        ...Many plotting commands which cost a lot of computational time...
        plt.draw()
        plt.pause(1e-3)

        # Saving.
        writer['figure_0'].grab_frame()
        writer['figure_1'].grab_frame()

0 个答案:

没有答案