如何在基础地图上添加多个点
plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05,wspace=0.15,hspace=0.05)
axs = plt.subplot(111)
m = Basemap(llcrnrlon=-50,llcrnrlat=40.2,urcrnrlon=0,urcrnrlat=52.2,
resolution='i',projection='merc'ax = axs)
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color="grey")
m.drawmapboundary()
#to plot multiple points
i=1
lons=list()
lats=list()
lbl=list()
while i<15:
lons=list(df[Lon])
lats=list(df[Lat])
lbl=list(df[Site])
lons.append(i)
lats.append(i)
lbl.append(i)
x,y = m(lons, lats)
axs.plot(x, y, 'o', label=lbl)
i+=1
plt.show()
你能帮我解决一下这个循环的问题吗?df是我的数据框。Lon、Lat和Site是df中的列名。
2 个回答
0
我不太明白你为什么要用经度和纬度来创建列表,因为你是一个一个点地绘制的。
另外,我觉得你想给每个点加标签,这可以用 annotate 来实现。我会这样做:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05,wspace=0.15,hspace=0.05)
axs = plt.subplot(111)
m = Basemap(llcrnrlon=-50,llcrnrlat=40.2,urcrnrlon=0,urcrnrlat=52.2,
resolution='i',projection='merc', ax = axs)
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color="grey")
m.drawmapboundary()
df = [{'lon': -8, 'lat': 45, 'site': 'point1'},
{'lon': -16, 'lat': 46, 'site': 'point2'}]
for point in df:
x, y = m(point['lon'], point['lat'])
axs.annotate(point['site'], xy=(x, y), xycoords='data',
xytext=(-40, 20), textcoords = 'offset points',
arrowprops=dict(arrowstyle="->"))
axs.plot(x, y, marker='o')
plt.show()
1
使用
axs.plot(x, y, 'o', label=lbl)
而不是
m.plot(x, y, 'o', label=lbl)