2016-08-23 114 views
2

我寫了一個簡單的腳本,基於代碼herematplotlib - 更快的幀速率?

我只需要顯示點,不需要線。

下面的腳本正確顯示的點,但我想看到的點在假設30fps的幀速率移動更快,在可配置的方式。

import matplotlib 
matplotlib.use('TKAgg') 

import numpy as np 
from matplotlib import pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.colors import cnames 
from matplotlib import animation 

NbOfObjects = 3 

fig = plt.figure() 

ax = fig.add_axes([0, 0, 1, 1], projection='3d') 
ax.axis('on') 

ax.set_xlim((-1.5, 1.5)) 
ax.set_ylim((-1.5, 1.5)) 
ax.set_zlim((0, 1.5)) 

ax.set_xlabel('x axis') 
ax.set_ylabel('y axis') 
ax.set_zlabel('z axis') 

# set point-of-view: specified by (altitude degrees, azimuth degrees) 
ax.view_init(30, 30) 

# choose a different color for each trajectory 
colors = plt.cm.jet(np.linspace(0, 1, NbOfObjects)) 

# set up points 
pts = sum([ax.plot([], [], [], 'o', c=c) 
      for c in colors], []) 

data = [[[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 0.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[1.0, 1.0, 0.0], [0.0, 0.0, 1.0], [-1.0, -1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]], 
     [[0.0, 0.0, 0.0], [-1.0, -1.0, 1.0], [1.0, 1.0, 1.0]]] 


# initialization function: plot the background of each frame 
def init(): 
    for pt in pts: 
     pt.set_data([], []) 
     pt.set_3d_properties([]) 

    return pts 


def animate(i): 

    print "i: ", i 

    for pt, positon in zip (pts, data[i]): 

     x = positon[0] 
     y = positon[1] 
     z = positon[2] 

     pt.set_data(x, y) 
     pt.set_3d_properties(z) 

    fig.canvas.draw() 

    return pts 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, frames=len(data), blit=True, repeat=False) 

plt.show() 
  1. 如何在屏幕上的幀的顯示涉及在animation.FuncAnimation「間隔」參數? (我不能讓它在不到5秒的時間內顯示50幀)

  2. 此設計是處理長時間潛在30fps大量點(10+)的最佳方式嗎? (「數據」在1小時內會很大)。

我還在研究各種示例,但沒有顯示簡單的3D動畫散點圖。

謝謝。

+0

你可能會更好地在一次調用中繪製所有點,傳遞一個顏色數組作爲'c ='參數而不是一個對象。雖然我在每幀只有3分猜測,它可能沒有什麼區別 – Eric

+0

我想'塊傳輸= TRUE'使得'fig.canvas.draw()'不必要 – Eric

+0

這似乎這樣的伎倆!我還沒有做出確切的測量,但我相信現在可以達到30 fps。這太棒了,謝謝。 – lolo

回答

1
  1. the documentation

    kwargs include repeat , repeat_delay , and interval : interval draws a new frame every interval milliseconds. repeat controls whether the animation should repeat when the sequence of frames is completed. repeat_delay optionally adds a delay in milliseconds before repeating the animation.

    然而,這僅設置中的幀速率的上限 - 如果時間過長畫一個框,然後你會看到一個較慢的幀速率

  2. 如果你點的數量較多,你想要做的事,如:

    data = np.array(data) 
    
    # in init, get a single mpl_toolkits.mplot3d.art3d.Line3D object 
    # the comma is important! 
    pts, = ax.plot([], [], [], 'o', c=colors) 
    
    # in update() 
    pts.set_data(data[i,:,0], data[i,:,1]) 
    pts.set_3d_properties(data[i,:,2]) 
    

    因此,你可以消除for循環

+0

2 /我現在明白了逗號的重要性(將列表的第一個元素分配給pts)。無論如何,我認爲我明白了你的想法......我根據你的建議修改了2個函數,但是在update()中我現在得到**「AttributeError:'list'object has no attribute'set_data'**這是有道理的由於PTS是一個列表。 我不知道如何做到這一點不使用(另)一個「for」循環。我會盡力找到文檔中的一些信息。THX。 – lolo

+0

'pts'是在Line3D對象我的機器...或該評論的第一行凌駕它> – Eric

+0

@lolo休息:在方向上的差異 – Eric