[FuncAnimation在动态发送数据以绘制散点图后不响应

问题描述 投票:0回答:1

因此,我从matplotlib使用FuncAnimation,以便动态绘制从串行端口到达的一些数据(在我的项目中,是dronekit的车辆类,用绿色圆点显示),我基本上拥有的动画称为每个循环都接收到更改了数据的新的车辆类别,因此可以对其进行绘制,但是由于某种原因,它会在执行任务的线程之后几秒钟后进行绘制(这会弹出“刷新”车辆数据起并杀死python(死亡之轮),这就是我得到的:

enter image description here

我在FuncAnimation开始运行时在调用的函数中放置了一些跟踪打印,如下所示:

def droneAnimation(i, vehicle, droneScatter):
     time.sleep(1)
     lat = [vehicle.location.global_relative_frame.lat]
     lon = [vehicle.location.global_relative_frame.lon]
     alt = [vehicle.location.global_relative_frame.alt]
     print("Alt received: " + str(alt))
     droneScatter._offsets3d = (lat,lon,alt)
     print("Changed pos")

您可以看到,这些打印在最初的几秒钟内被触发,但在经过几次迭代后仍然崩溃。FuncAnimation的调用方式如下:

        fig,droneScatter = plotLiveSimpleFacade(vehicle,w,2)
        ani = FuncAnimation(fig,droneAnimation, fargs = (vehicle,droneScatter))
        plt.draw()
        plt.pause(0.1)
        m = threading.Thread(target=MissionStart(vehicle,hmax) , name = "MISSION")
        m.start()

供参考:图是plt.figure(),droneScatter只是一个分散点,车辆是包含动态更新数据的车辆类别,MissionStart线程只是使车辆类别随时间变化的线程。

我也要提到,无花果处于交互模式,并且轴限制设置得很好(我看到,当您动态更改数据但不缩放轴时,可能会有问题),尝试不同的组合plt.draw()和plt.plot(block = False)导致我根本不绘制图或仅绘制空白图。

因为我不知道是什么原因造成的,所以我将dronekit标签放在此标签上,并在线程中查看是否有人有任何想法!

matplotlib animation python-multithreading mplot3d dronekit
1个回答
0
投票
我已经研究过使用matplotlib进行线程化,并且看起来像使用该库进行线程化并不是最好的,因为它不是线程安全的,最好的选择是使用python进行多处理或以其他方式解决问题。您可以在此post中找到更多信息。
© www.soinside.com 2019 - 2024. All rights reserved.