底图和缩放边界
点击“搜索”按钮并放大地图上的某个区域后,如何获取该显示区域边界的经纬度地理坐标?
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()
After clicking on the ’Search‘ button and zooming in on a region of the map, how do you get the geographic coordinates in longitude and latitude of the borders of this displayed region?
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绘图限制可通过
Axes
对象中的get_xlim
和get_ylim
方法获得:这些限制位于投影坐标中,您可以将它们转换为通过使用附加关键字参数
inverse=True
调用Basemap
对象来获取地理坐标:The plot limits are available through the methods
get_xlim
andget_ylim
from yourAxes
object:These limits are in projection coordinates, and you can convert them to geographic coordinates by calling your
Basemap
object with the additional keyword argumentinverse=True
:感谢莫利纳夫,
如果我将您的说明放在程序中,它为Latmin和latmax提供了错误的值,但是如果我更改 on_ylim_change()的函数on_xlim_change(),则使用 ax.callbacks.connect(' ylim_changed',on_ylim_change)一切变得完美。
我不知道为什么和何时,但这就是这样。
再次感谢。
Thanks Molinav,
If I put your instructions in my program, it gives me wrong values for latmin and latmax, but if I change the function on_xlim_change() in on_ylim_change() with ax.callbacks.connect(‘ylim_changed‘,on_ylim_change) everything becomes perfect.
I don't know why and wherefore, but that's the way it is.
Thanks again.