Skip to main content
 首页 » 编程设计

python之matplotlib之更快的帧率

2025年04月02日9rubylouvre

我已经根据 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. 屏幕上帧的显示与 animation.FuncAnimation 中的“interval”参数有什么关系? (我无法让它在不到 5 秒内显示 50 帧)

  2. 这种设计是否是在很长一段时间内以可能达到 30fps 的速度处理大量点 (10+) 的最佳方式? (“数据”对于 1 小时的持续时间来说会相当大)。

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

谢谢。

请您参考如下方法:

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