from pymavlink import mavutil
import matplotlib.pyplot as plt
plt.switch_backend('TkAgg')
# Connect to Mission Planner
master = mavutil.mavlink_connection('udpin:localhost:14550')
# Create empty lists to store data
timestamps = \[\]
latitudes = \[\]
longitudes = \[\]
# Set up the plot
plt.figure()
fig, ax = plt.subplots()
line, = ax.plot(\[\], \[\], 'b-')
ax.set_xlabel('Time')
ax.set_ylabel('Latitude')
plt.grid()
plt.draw() # draw the plot
# Function to update plot
def update_plot(timestamps, latitudes):
line.set_xdata(timestamps)
line.set_ydata(latitudes)
ax.relim()
ax.autoscale_view()
fig.canvas.flush_events()
fig.canvas.blit() # or draw()
fig.canvas.start_event_loop(0.001) #1 ms seems enough
# Main loop
while True:
# Wait for a message
msg = master.recv_match()
if msg is not None:
# Check for GPS message
if msg.get_type() == 'GLOBAL_POSITION_INT':
# Extract latitude and longitude
lat = msg.lat / 1e7
lon = msg.lon / 1e7
timestamps.append(msg.time_boot_ms)
latitudes.append(lat)
# Update plot
update_plot(timestamps, latitudes)
print(f"GPS Position: Latt={lat:.6f}, Lonn={lon:.6f}")
That is my code, I updated matplotlib and i changed the backend but it is always not responding. please help me resolve this problem . It's frustrating when even after updating matplotlib and changing the backend, my plot remains unresponsive.