简体   繁体   中英

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:

在此处输入图像描述

I've put some tracking prints inside the function that is called when the FuncAnimation starts running, looks like this:

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:

        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.

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.

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!

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. You can find more information at this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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