使用matplotlib循环绘图



是否有一种方法可以使matplotlib上制作的情节像电影一样循环?如果我有12个不同的情节,让它播放第一个10秒,然后是第二个,以此类推,然后在最后一个之后重复播放?由于我想要显示的图的数量,我想将此作为子图的替代方法。

yes,使用动画模块与repeat = True,并通过修改interval参数来控制情节之间的时间。

import matplotlib.animation as animation
fig = plt.figure()#

data_, = plot([], [])

def data_gen():
    while i < NUMBER_OF_PLOTS:
        '''
         make your instance of X data in a list called YOUR_X_DATA
         and your Y data in a list called YOUR_Y_DATA
         and iterate along it.
        '''
        yield YOUR_X_DATA[i], YOUR_Y_DATA[i]
        i+=1
def run(data):                
    data_.set_data(data[0], data[1])
ani = animation.FuncAnimation(fig , run, data_gen, interval=100,repeat=True)
plt.show()

OR使用time.sleep(10)plt.close(fig)

import time 
# suppose you have fig1, fi2, fig 3 etc. 
while(True):
   time.sleep(10)
   plt.close(fig1)
   # import the next plot
   time.sleep(10)
   plt.close(fig2)

   # import the next plot
   time.sleep(10)
   plt.close(fig3)

最新更新