'动画'2D散射

时间:2012-10-20 21:05:37

标签: python animation matplotlib

我想要想象过滤器的转换。我想绘制一个散点图,其中每半秒绘制下一个滤镜的值。

我的目标是:

  1. 将所有值绘制到点(k),但要在图上标出值(k)。

  2. 在(k)和(k + 1)的绘图值之间暂停

  3. 全屏绘图

  4. 完成所有迭代后的情节

  5. 我做了一个函数,但效率很低,在绘制一些值后一切都变慢了。

    我找到的唯一方法是使用交互式情节ion(),并且每一步都使用更新的标记再次绘制所有点。对于每个步骤(k),我想删除先前的点(k-1)并用不同的标记添加它们并添加当前点(k)

    import pylab as pl
    import time
    xPos1 = pl.arange(100)
    m1 = [pl.sin(pl.pi*x/10) for x in xPos1]
    m2 = [pl.cos(pl.pi*x/30) for x in xPos1]
    m3 = [pl.sin(pl.pi*x/20) for x in xPos1]
    trueVal1 = [0 for real in xPos1] 
    
    def conversionAnim(xPos, trueVal, *args):    
        mTuple = [arg for arg in args]
        colorList = ['Green','Blue','Orchid','Cyan','Goldenrod','Salmon','Orange','Violet','Magenta']
        f = pl.figure(figsize =(17,8))
        pl.ion()
        pl.xlim(min(xPos)-1, max(xPos)+1)
        pl.ylim(min(j for i in mTuple for j in i)-.5, max(j for i in mTuple for j in i)+.5)
        for i in range(len(xPos)):
            print '\ni = %i' % i 
            for j in range(len(mTuple)):
                m = mTuple[j]            
                mVal = [element for element in m] 
                print 'Value%i is %s' %(j,mVal[i])       
                if i == 0:
                    pl.hold(True)
                    pl.scatter(xPos[i],mVal[i],s=50, marker = 'o', color = 'Dark'+colorList[j])
                    pl.plot(xPos[i],trueVal[i])                
                else:
    
                    pl.scatter(xPos[i],mVal[i],s=50, marker = 'o',color = 'Dark'+colorList[j])                
                    pl.scatter(xPos[i-1], mVal[i-1],s=50, marker = 'o', color = 'white')
                    pl.scatter(xPos[i-1], mVal[i-1],s=50, marker = 'x', color = colorList[j])                
                    pl.plot(xPos[i-1:i+1],trueVal[i-1:i+1], color = 'red')       
    
                pl.draw()
            time.sleep(.01)
        time.sleep(3)  # to hold figure after its shown
    
    if __name__ == '__main__':
        conversionAnim(xPos1, trueVal1, m1, m2, m3)
    

    我不知道如何绕过ion()并使此功能高效。

1 个答案:

答案 0 :(得分:0)

使这更有效的最简单方法是使用2N线图而不是大量scatter图。 (看起来你最终得到每个数据点的三个散点图!)

作为旁注,您有几行(mTuple = [arg for arg in args])将元组转换为list s。写mTuple = list(args)更清楚,但我认为你实际上并不需要做那些转换,因为你只需要一个可迭代的东西。

import itertools

def covnersion_Anim(xPos,trueVal,*args):
    mTuple = args
    plt_bulk_lst = []
    plt_head_lst = []
    color_list = ['Green','Blue','Orchid','Cyan','Goldenrod','Salmon','Orange','Violet','Magenta']
    f = plt.figure(figsize =(17,8))
    ax = plt.gca()
    ax.set_xlim([min(xPos),max(xPos)])
    ax.set_ylim([0,1])
    ms = 5
    for j,c in zip(range(len(mTuple)),itertools.cycle(color_list)):
        plt_bulk_lst.append(ax.plot([],[],color=c,ms=ms,marker='x',linestyle='none')[0])
        plt_head_lst.append(ax.plot([xPos[0]],[mTuple[j][0]],color='Dark'+c,ms=ms,marker='o',linestyle='none')[0])
    real_plt, = plot([],[],color='red')

    for j in range(1,len(xPos)):
        print j
        for hd_plt,blk_plt,m in zip(plt_head_lst,plt_bulk_lst,mTuple):
            hd_plt.set_xdata([xPos[j]])
            hd_plt.set_ydata([m[j]])

            blk_plt.set_ydata(m[:j])
            blk_plt.set_xdata(xPos[:j])

            real_plt.set_xdata(xPos[:j])
            real_plt.set_ydata(trueVal[:j])

        plt.pause(1)

    return f
covnersion_Anim(range(12),rand(12),rand(12),rand(12),rand(12))