Matplotlib.animation没有更新

时间:2014-11-18 18:23:43

标签: python animation matplotlib pickle

需要有关此代码段的帮助:我有一组pickle数据。文件名为EEG-ClenchingA.pickle,其中A是整数。此外,另一个代码通过串行端口连续读取数据,并且每次读取一组新数据时(一组我的意思是一分钟长数据=有15个通道和128 Hz采样率=> 128 * 60 = 7680即每分钟我得到一个7680 * 15的数组作为输入。),我在另一个index.pickle文件中递增计数器。让我们说index.pickle中的计数器值现在是X.然后下一个数据集为1 min read保存为EEG-ClenchingX.pickle。然后index.pickle中的计数器值更新为X + 1所以在这里,我写了一个代码,每分钟生成一次数据。可悲的是,我的动画只更新了一次,虽然数据文件存在,而index.pickle中的计数器每分钟更新一次(我可以看到它发生)。

请帮助!

代码:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pickle
import numpy

rate   = 128     #sampling rate of epoc.
time   = 60  #duration of sampling as defined in epoc_collect.py code.

fig    = plt.figure(1)
ax1    = fig.add_subplot (511)
ax2    = fig.add_subplot (512)
ax3    = fig.add_subplot (513)
ax4    = fig.add_subplot (514)
ax5    = fig.add_subplot (515)

fig    = plt.figure(2)
ax6    = fig.add_subplot (511)
ax7    = fig.add_subplot (512)
ax8    = fig.add_subplot (513)
ax9    = fig.add_subplot (514)
ax10   = fig.add_subplot (515)

fig    = plt.figure(3)
ax11   = fig.add_subplot (411)
ax12   = fig.add_subplot (412)
ax13   = fig.add_subplot (413)
ax14   = fig.add_subplot (414)


def animate(i):
    print 'value of i'
    print i

    index_name = 'index.pickle'
    with open(index_name) as f:
            index = pickle.load(f)
    index    = int(index) -1
    index    = str(index)

    print 'index no:'
    print index
    filename = 'EEG-Clenching'+ index +'.pickle'
    pullData = pickle.load( open( filename, "rb" ) )

    ref_val  = []
    channel1 = []
    channel2 = []
    channel3 = []
    channel4 = [] 
    channel5 = []
    channel6 = []
    channel7 = []
    channel8 = []
    channel9 = []
    channel10= []
    channel11= []
    channel12= []
    channel13= []
    channel14= []

    x       = rate*time
    for j in range (x) :
        ref_val.append((j*128) + int(pullData[j,0]))
        channel1.append(int(pullData[j,1]))
        channel2.append(int(pullData[j,2]))
        channel3.append(int(pullData[j,3]))
        channel4.append(int(pullData[j,4]))
        channel5.append(int(pullData[j,5]))
        channel6.append(int(pullData[j,6]))
        channel7.append(int(pullData[j,7]))
        channel8.append(int(pullData[j,8]))
        channel9.append(int(pullData[j,9]))
        channel10.append(int(pullData[j,10]))       
        channel11.append(int(pullData[j,11]))
        channel12.append(int(pullData[j,12]))
        channel13.append(int(pullData[j,13]))
        channel14.append(int(pullData[j,14]))


    ax1.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    ax5.clear()
    ax6.clear()
    ax7.clear()
    ax8.clear()
    ax9.clear()
    ax10.clear()
    ax11.clear()  
    ax12.clear() 
    ax13.clear()     
    ax14.clear()     

    ax1.plot(ref_val,channel1)  
    ax2.plot(ref_val,channel2)
    ax3.plot(ref_val,channel3)
    ax4.plot(ref_val,channel4)
    ax5.plot(ref_val,channel5)
    ax6.plot(ref_val,channel6)
    ax7.plot(ref_val,channel7)
    ax8.plot(ref_val,channel8)
    ax9.plot(ref_val,channel9)
    ax10.plot(ref_val,channel10)
    ax11.plot(ref_val,channel11)
    ax12.plot(ref_val,channel12)
    ax13.plot(ref_val,channel13)
    ax14.plot(ref_val,channel14)    

animation.FuncAnimation(fig,animate,frames = 30,interval=60000)
plt.show()

0 个答案:

没有答案