我正在使用带有动画的底图绘制飞机经纬度的位置,以刷新地图。我还希望用航班ID标记地块。从连续更新的文本文件中读取数据位置。 对于这篇文章,我已经用'position_data'替换了文本文件,并且排期ID是固定的,但是结果是一样的。 如果我注释掉plt.text行,则正确绘制了点,但是如果包含此行,则会出现错误“ TypeError:float()参数必须为字符串或数字” 取消注释x2 = 3000000和y2 = 3000000会同时绘制点和标签(尽管在错误的位置,因为这通常是对标签的测试) 对于解决这个问题的任何帮助将不胜感激,因为python和map对我来说是新的。
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import os.path
import matplotlib.animation as animation
my_map = Basemap(projection='merc',llcrnrlat = 20,llcrnrlon = -100,urcrnrlat = 60,
urcrnrlon = 20,resolution='l')
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color = '#D2FFC4')
my_map.drawmapboundary()
my_map.drawparallels(np.arange(10,90,20),labels=[1,1,0,1])
my_map.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1])
x1=[]
y1=[]
x2,y2 = my_map(0, 0)
point = my_map.plot(x2, y2, 'r.', markersize=1)[0]
def init():
point.set_data([], [])
return point,
def get_position():
position_data= ['10.18,50.7\n', '8.6,49.9\n','5.0,52.7\n', '4.7,52.36\n', '11.1,56.58\n' ]
#print position_data #debug
for line in position_data:
if len(line)>0:
x,y=line.split(',')
#print x,y #debug
x1.append(float(x))
y1.append(float(y))
#print x1,y1 #debug
return x1,y1
# animation function.
def animate(i):
get_position()
x2,y2=my_map(x1,y1,inverse=False)
#x2=3000000
#y2=3000000
#print x2
plt.text(x2, y2, 'AB123',fontsize=6,fontweight='bold', ha='left',va='bottom',color='r')
point.set_data(x2, y2)
return point,
# call the animator.
anim = animation.FuncAnimation(plt.gcf(), animate, init_func=init,
frames=200, interval=1000, blit=False)
plt.show()
感谢有用的提示。我在用变量混淆列表! 已更正且有效的代码为:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import os.path
import matplotlib.animation as animation
my_map = Basemap(projection='merc',llcrnrlat = 20,llcrnrlon = -100,urcrnrlat = 60,
urcrnrlon = 20,resolution='l')
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color = '#D2FFC4')
my_map.drawmapboundary()
my_map.drawparallels(np.arange(10,90,20),labels=[1,1,0,1])
my_map.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1])
x1=[]
y1=[]
idx=0
ids=[]
p=0
position_data=[]
x2,y2 = my_map(0, 0)
point = my_map.plot(x2, y2, 'r.', markersize=1)[0]
def init():
point.set_data([], [])
return point,
def get_position():
position_data= ['10.18,50.7,ab123\n', '8.6,49.9,bc234\n','5.0,52.7,cd456\n', '4.7,52.36,ef678\n', '11.1,56.58,gh789\n' ]
global idx
idx=0
for line in position_data:
if len(line)>0:
x,y,id=line.split(',')
print x,y,id #debug
x1.append(float(x))
y1.append(float(y))
ids.append(id)
idx=idx+1
#print idx #debug
#print x1,y1,ids #debug
return
# animation function.
def animate(i):
get_position()
x2,y2=my_map(x1,y1,inverse=False)
#print idx
point.set_data(x2, y2)
for p in range(idx):
plt.text(x2[p],y2[p],ids[p],fontsize=7,ha='left',va='bottom',color='k')
# point.set_data(x2, y2)
del x1[:] # or lists grow every loop!
del y1[:]
del ids[:]
return point,
# call the animator.
anim = animation.FuncAnimation(plt.gcf(), animate, frames=20, interval=1000, blit=False)
plt.show()