I am trying to create an animated plot of a series of points with lat/lon positions on a matplotlib.basemap
map. Each point has a series of positions for a series of days, which I have read into a pandas
DataFrame
.
I've tried to modify the procedure used HERE to do this, but I am getting an error that global name 'points' is not defined
. I've tried to declare this as a global within the init
routine, but that didn't help.
How might I do this?
Example data:
day,id, lon, lat
156, 1, 67.53453, -4.00454
156, 2, 66.73453, 0.78454
156, 3, 68.23453, -1.01454
157, 1, 67.81453, -4.26454
157, 2, 66.42653, 0.91454
157, 3, 69.11253, -1.01454
158, 1, 68.12453, -3.26454
158, 2, 67.10053, 1.01454
158, 3, 68.01253, -2.61454
Calling routine:
if datafile != None:
data = readdata(datafile)
dates = np.unique(data.daynr).values
x,y = m(0,0)
point = m.plot(x,y, 'ro', markersize=5)[0]
points = list()
anim = animation.FuncAnimation(plt.gcf(), animate,
init_func=init, frames=20,
interval=500, blit=True)
# Add current date/time or something to make unique
anim.save('movement.mp4', fps=15,
extra_args=['-vcodec', 'libx264'])
init
, animate
, and data reading routines:
def init():
for pt in points:
pt.set_data([], [])
return points
def animate(i):
lons = data.lons[data.daynr==dates[i]]
lats = data.lats[data.daynr==dates[i]]
i = 0
for lon,lat, pt in zip(points, lons, lats):
x, y = map(lon,lat)
pt.set_data(x, y)
i = i + 1
return points
def readdata(datafile):
dtypes = np.dtype([
('daynr',int), #00 - Simulation day number
('id',int), #01 - Id
('lon',float), #02 - Longitude
('lat',float), #03 - Latitude
])
f = open(datafile, 'rb')
data = pd.read_csv(f, index_col=False, names=dtypes.names,
dtype=dtypes, header=None)
f.close()
return data