0

I am trying to make a virtual flight tracker with Python and API data, and I have managed so far to draw the points, update their location every 10 seconds and annotate them. Unfortunately, they are being annotated for each position they are drawn in when in fact I want them to only be shown in the spot where the point is at that time, or else there is a constant trail of labels.

The code is below:

import requests
import json
import time
import cartopy.crs as ccrs
import cartopy
import matplotlib.pyplot as plt
from cartopy.io.img_tiles import GoogleTiles
from matplotlib import animation

#SET AXES
fig, ax = plt.subplots()
ax=plt.axes(projection=ccrs.PlateCarree())

'''
ax.set_ylim(48,61.5)
ax.set_xlim(-12.5, 3.3)
'''



#ADD OSM BASEMAP
osm_tiles=GoogleTiles(style='satellite')
ax.add_image(osm_tiles,8)
ax.stock_img()
ax.set_extent([-12.5, 3.3, 48, 61.55])
ax.add_feature(cartopy.feature.BORDERS)

#mplcursors.cursor(hover=True)

#PLOT TRACK
track, = ax.plot([], [], 'wo')

# RELOAD API EVERY 15"

def update (self):
    vapi = requests.get("https://data.vatsim.net/v3/vatsim-data.json")

    # LOAD DATA AS JSON
    data = vapi.text
    parse_json = json.loads(data)

    # LOAD PILOTS
    pilots = parse_json['pilots']
    no_pilots = len(pilots)


    # GET INFO FOR EACH AIRCRAFT
    x = 0
    callsigns, lat, lon, alt, head, gspeed = [], [], [], [], [], []

    while x < no_pilots:

        xcall = pilots[x]['callsign']
        xlat = pilots[x]['latitude']
        xlon = pilots[x]['longitude']
        xgspeed = pilots[x]['groundspeed']
        xalt = pilots[x]['altitude']
        xhead = pilots[x]['heading']

        callsigns.append(xcall)
        lat.append(xlat)
        lon.append(xlon)
        alt.append(xalt)
        head.append(xhead)
        gspeed.append(xgspeed)

        x += 1


    for i, txt in enumerate(callsigns):
        ax.annotate(txt, (lon[i], lat[i]))
        

    track.set_data(lon,lat)
    return track, callsigns,

anim = animation.FuncAnimation(fig, update,interval=10000, blit=False)

plt.show()

It leaves a trail of annotations like this

  • you probably need to explicitly clear the annotations, here's a nice SO answer that may help: https://stackoverflow.com/a/42315650/9357244 – chris Mar 28 '22 at 21:32

0 Answers0