在matplotlib“非阻塞”中生成animation.FuncAnimation()

2024-10-02 08:16:27 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是关于这一主题的有用建议: How to pass arguments to animation.FuncAnimation()? 用于将参数传递到maptlotlib

我的问题是我有一个在200Hz下生成(x,y)坐标的设备

我想以大约20Hz的较慢速率绘制x,y位置

我的代码现在的问题是,在获得第一组(x,y,)点后,它进入animation.FuncAnimation函数并保持不变。相反,我试图保持(x,y,)数据以200 Hz的频率运行,并且只让animation.FuncAnimation以所需的较慢速度运行

我的代码如下所示:

#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import style

style.use('dark_background')

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(-1, 1), ylim=(-1, 1))
line, = ax.plot([], [], lw=2)
pointsFollower, = ax.plot([], [], marker='o', color = 'C0',ls="")





def init():
    pointsFollower.set_data([], [])
   
    return pointsFollower, 

def animate(i,factor):
    xFollower = (factor[0])
    yFollower = (factor[1])
    print("in anim loop:",xFollower,yFollower)
    pointsFollower.set_data(xFollower,yFollower)
    
    return pointsFollower,


def main():
    while True:
        # Wait for the next set of frames from the camera
        ##function here sets pipeline to device

        # Fetch pose frame
        ##function here gets x and y data.
        
        K = (data.translation.x,data.translation.y)
       
    
        anim = animation.FuncAnimation(fig, animate, fargs=(K,), init_func=init,
                                frames=200, interval=20, blit=True)

        plt.show()
    

    pipe.stop()



if __name__ == '__main__':
    main()

Tags: thetofromimportdataplotmatplotlibdef

热门问题