简体   繁体   English

动态发送数据到 plot 移动散点后,FuncAnimation 没有响应

[英]FuncAnimation doesn't respond when after dynamically sending data to plot to move a scatter point

So I'm using FuncAnimation from matplotlib so dynamically plot some data as it arrives from a serial port (in my project is the vehicle class from dronekit, which is displayed with the green dot), what I have basically is the animation called which every loop is receiving a new vehicle class with data changed so it can be plotted, but for some reason it plots but after a couple of seconds later after the thread of the mission(which allows the "refresh" of the vehicle data it pops up and kills python (Wheel of death), here's what I get: So I'm using FuncAnimation from matplotlib so dynamically plot some data as it arrives from a serial port (in my project is the vehicle class from dronekit, which is displayed with the green dot), what I have basically is the animation called which every循环正在接收一辆新车 class,其数据已更改,因此可以绘制,但由于某种原因,它绘制但在任务线程后几秒钟后(允许“刷新”它弹出的车辆数据和杀死 python (死亡之轮),这是我得到的:

在此处输入图像描述

I've put some tracking prints inside the function that is called when the FuncAnimation starts running, looks like this:我在 FuncAnimation 开始运行时调用的 function 中放置了一些跟踪打印,如下所示:

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")

As you can see those prints are triggered the first few seconds but still crashes after a few iterations.如您所见,这些打印在最初的几秒钟内被触发,但在几次迭代后仍然崩溃。 The FuncAnimation is called like this: 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()

For reference: fig is a plt.figure(),droneScatter is just a scatter point, vehicle is the vehicle class containing the data that dynamically updates and the MissionStart thread is just a thread to make the vehicle class change overtime.供参考: fig 是一个 plt.figure(),droneScatter 只是一个散点,vehicle 是车辆 class 包含动态更新的数据,MissionStart 线程只是一个线程,使车辆 class 超时变化。

I would like to mention as well that the fig is on interactive mode on, and the axes limits are set well (I saw that when you dynamically change data but don't scale the axes may have problems) also, trying different combinations of plt.draw() and plt.plot(block = False) leads me to not plotting at all or just a blank plot.我还想提一下,无花果处于交互模式,并且轴限制设置得很好(我看到当您动态更改数据但不缩放轴时可能有问题),尝试不同的 plt 组合.draw() 和 plt.plot(block = False) 导致我根本不绘图,或者只是一个空白的 plot。

Since I have no idea of what is causing this I'll put the dronekit tag on this and the threading to see if anyone has any idea!因为我不知道是什么原因造成的,所以我会在这个和线程上贴上dronekit标签,看看是否有人知道!

I've looked onto threading with matplotlib and looks like threading with this said library it's not the best as it's not thread safe, the best bet is to look at multiprocessing with python or approaching the problem in a different manner.我已经查看了 matplotlib 的线程,并且看起来使用这个所说的库进行线程并不是最好的,因为它不是线程安全的,最好的办法是查看 python 的多处理或以不同的方式解决问题。 You can find more information at this post您可以在这篇文章中找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM