Skip to main content
 首页 » 编程设计

python之单独循环中的matplotlib动画控制间隔

2024年10月01日6kerrycode

短

有没有一种直接的方法可以在脚本运行时精确控制动画对象的显示时间,同时考虑到 updatefig 函数的每个循环所花费的时间?

长

我正在编写一个应该在 Raspberry Pi 上运行的脚本。它拍摄竞技场的照片并跟踪移动的点。

用户应该有的一个选项是查看程序在每一帧执行的操作。显而易见的方法是使用 matplotlib 的动画模块,因为这应该是最快的绘图方法。为了在实验后自动关闭绘图并继续保存数据等。我希望脚本在给定时间(帧数/帧率)后关闭绘图

我找到了一种让情节自行关闭的方法,方法是使用 plt.ion() 运行脚本,然后在调用动画后仅使用 plt.pause()。

现在,什么停顿?似乎 plt.pause() 没有考虑在 updatefig() 函数的每个循环上花费的时间。所以我不得不定义每个循环在 调用 plt.pause 之前需要多长时间。

为了说明这个问题,我写了一个简短的示例代码(您可能需要更改 plt.pause 的行,具体取决于您机器的速度 - 0.55 是这个循环在我的计算机上的平均速度)。

import matplotlib.pyplot as plt 
from matplotlib import animation 
import numpy as np 
import time 
import random 
 
plt.ion() 
 
class example(): 
    ''' 
    In this example I want to illustrate that I don't know a good way to control  
    the timing of the animation module. 
    I want to have 10 frames with an interval of 10 seconds, so I would think  
    that I only have to call plt.pause(10) after opening the animation. 
    Unfortunately it seems that the animation.Funcanimation is essentially  
    blocking the plt.pause function when it's still in the loop. 
    ''' 
    def __init__(self): 
 
        self.image = np.zeros((1000,1000), dtype=np.float32) 
        self.total_time = 0 
 
        self.plotting() 
 
    def plotting(self): 
 
        fig = plt.figure(figsize=(7,7)) 
        ax = fig.add_subplot(111) 
        video_object = ax.imshow(self.image, vmin=0, vmax=1000, animated=True) 
 
        def init(): 
            self.image[:, :] = np.random.rand(1000, 1000) 
            video_object = ax.imshow(self.image) 
            return(video_object,) 
 
        def updatefig(i): 
            start_time = time.time() 
            #self.image[:,:] = np.random.rand(1000,1000) # I leave this here as this is how it should be done 
            for i in range(self.image.shape[0]): # slow looping  
                for j in range(self.image.shape[1]): 
                    self.image[i,j] = random.uniform(0,1000) 
 
            video_object.set_array(self.image) 
            self.total_time += time.time() - start_time 
            print('time in loop: ' + repr(time.time() - start_time)[0:5]) 
            return(video_object,) 
 
        ani = animation.FuncAnimation(fig = fig, func = updatefig, init_func = init, 
                                      frames = 10, interval=1000, blit=True, 
                                      repeat=False) 
        fig.show() 
        plt.pause(10-(0.55*10)) 
        plt.close() 
        print('Total time loop : ' + repr(self.total_time)) 
 
outer_time = time.time() 
example() 
print('Total time until close fig: ' + repr(time.time() - outer_time)) 

请您参考如下方法:

将交互模式与 FuncAnimation 混合可能不是一个好主意。相反,您可以使用计时器 (fig.canvas.new_timer(interval=interval)) 在 interval 毫秒后调用 plt.close()。这里的 interval 是帧数乘以重复时间(或动画间隔),再加上一个小的偏移量,这可能是实际创建窗口所花费的时间。 在下面的示例中,这被选择为 300 毫秒,这对我来说很好。

import matplotlib.pyplot as plt 
from matplotlib import animation 
import numpy as np 
import time 
import random 
 
 
class example(): 
 
    def __init__(self, nframes = 17, interval = 1000): 
 
        self.image = np.zeros((1000,1000), dtype=np.float32) 
        self.total_time = 0 
        self.nframes = nframes 
        self.interval = interval 
        self.plotting() 
 
    def plotting(self): 
 
        fig = plt.figure(figsize=(7,7)) 
        ax = fig.add_subplot(111) 
        video_object = ax.imshow(self.image, vmin=0, vmax=1000, animated=True) 
        #creating a timer object and setting an interval of  
        # nframes * interval + time to first draw milliseconds 
        timer = fig.canvas.new_timer(interval = self.nframes*self.interval+300)  
        timer.add_callback(plt.close) 
 
        def init(): 
            self.image[:, :] = np.random.rand(1000, 1000) 
            video_object = ax.imshow(self.image) 
            # start timer 
            timer.start() 
            return(video_object,) 
 
        def updatefig(k): 
            start_time = time.time() 
            #self.image[:,:] = np.random.rand(1000,1000) # I leave this here as this is how it should be done 
            for i in range(self.image.shape[0]): # slow looping  
                for j in range(self.image.shape[1]): 
                    self.image[i,j] = random.uniform(0,1000) 
 
            video_object.set_array(self.image) 
            self.total_time += time.time() - start_time 
            print('time in loop: ' + repr(time.time() - start_time)[0:5] + "- " +str(k)) 
            return(video_object,) 
 
        ani = animation.FuncAnimation(fig = fig, func = updatefig, init_func = init, 
                                      frames = self.nframes, repeat=False, 
                                      interval=self.interval, blit=True ) 
 
        plt.show() 
 
        print('Total time loop : ' + repr(self.total_time)) 
 
outer_time = time.time() 
example(nframes = 17, interval = 1000) 
print('Total time until close fig: ' + repr(time.time() - outer_time))