底图和缩放边界

Basemap and zoom boundaries

点击“搜索”按钮并放大地图上的某个区域后,如何获取该显示区域边界的经纬度地理坐标?

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np


def on_xlim_change(*args):
    pass
    # Here I would like to get the coordinates in lat and long of the zoomed map

fig = plt.figure(figsize=(7,8),dpi=300,facecolor=(0.3,0.7,0.4,0.2))
ax = fig.add_subplot(111)

long1=-180; long2=180; lat1=-90; lat2=90
m = Basemap(projection='mill',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=long1,urcrnrlon=long2,resolution='i')
m.fillcontinents(color='coral',lake_color='aqua')
m.drawmapboundary(fill_color='aqua')
m.drawcoastlines(linewidth=0.3)
m.drawcountries(linewidth=0.15)
m.drawmeridians(np.arange(-180,180,30),dashes=[1,0],linewidth=0.1,labels=[False,False,True,False],fontname='Times New Roman',fontsize=4)
m.drawparallels(np.arange(-90,90,30),dashes=[1,0],linewidth=0.1,labels=[False,True,False,False],fontname='Times New Roman',fontsize=4)

ax.callbacks.connect('xlim_changed',on_xlim_change)

plt.show()

绘图限制可通过 get_xlimget_ylim 方法从您的 Axes 对象获得:

xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

这些限制在投影坐标中,您可以通过使用附加关键字参数 inverse=True:

调用您的 Basemap 对象来将它们转换为地理坐标
lonmin, latmin = m(xmin, ymin, inverse=True)
lonmax, latmax = m(xmax, ymax, inverse=True)

谢谢莫利纳夫, 如果我把你的指令放在我的程序中,它会给我错误的 latmin 和 latmax 值,但是如果我在 on_ylim_change() 中更改函数 on_xlim_change()使用 ax.callbacks.connect('ylim_changed',on_ylim_change) 一切都变得完美。 我不知道为什么和为什么,但事情就是这样。 再次感谢。