0
我想創建一系列帶有經緯度位置的一系列點在matplotlib.basemap
地圖上的動畫圖。每個點都有一系列的位置,我已經讀入pandas
DataFrame
。隨着時間的推移動畫Matplotlib底圖上的多個點
我試過修改使用的程序HERE來做到這一點,但我得到一個錯誤,global name 'points' is not defined
。我試圖在init
例程中聲明這是全局的,但這並沒有幫助。
我該怎麼做?
實施例的數據:
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
調用例程:
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
,和數據讀出的例程:
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
哪條線做你的程序中斷嗎? – waitingkuo
at'for pt in points:'in'init()' – ryanjdillon
你把所有的代碼放在一個文件中嗎? – waitingkuo