2012-10-20 117 views
0

我想要可視化過濾器的轉換。我想繪製一個散點圖,每隔半秒鐘繪製下一個過濾器的值。'動畫'2D散點圖

我的目標是:

  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()並使此功能有效。

+0

@cosmosis感謝您的評論。我最近問了幾個問題,儘管最被接受,但其中一些仍然沒有答案。這就是如何計算驗收的方式。對此無能爲力。 – tomasz74

回答

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))