matplotlib - 帧速率更快?

时间:2016-08-23 06:17:02

标签: python animation matplotlib

我根据here

的代码编写了一个简单的脚本

我只需要显示点,不需要任何行。

下面的脚本正确地显示了点,但我希望以可配置的方式看到点移动得更快,以帧速率为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. 屏幕上的帧显示如何与" interval" animation.FuncAnimation中的参数? (我无法在不到5秒的时间内显示50帧)

  2. 这种设计是否是长时间以30fps处理大量点(10+)的最佳方式? ("数据"在1小时内相当大)。

  3. 我还在研究各种示例,但没有一个展示简单的3D动画散点图。

    感谢。

1 个答案:

答案 0 :(得分:1)

  1. 来自the documentation

      

    kwargs包括repeatrepeat_delayintervalinterval每隔interval毫秒绘制一个新的帧。 repeat控制在帧序列完成时是否应重复动画。 repeat_delay可选地在重复动画之前添加延迟(以毫秒为单位)。

    然而,这仅设置帧速率的上限 - 如果绘制帧需要太长时间,那么您将看到较慢的帧速率

  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循环