0

我想可视化过滤器的转换。我想绘制一个散点图,每半秒绘制下一个过滤器的值。

我的目标是:

  1. 绘制直到点 (k) 的所有值,但要在绘图上指示值 (k)。

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

  3. 全屏绘图

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

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

我发现的唯一方法是使用交互式绘图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()并使此功能有效。

4

1 回答 1

0

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

作为旁注,您有几行 ( mTuple = [arg for arg in args]) 将元组转换为lists。写起来更清楚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))
于 2012-10-22T16:55:15.380 回答