【问题标题】:Matplotlib: Focus on specific lon/lat using spstere projectionMatplotlib:使用 spstere 投影关注特定的 lon/lat
【发布时间】:2017-12-31 07:58:01
【问题描述】:

我正在尝试使用 Python 中 matplotlib 包中的“spstere”投影将我的地图集中在南极洲的特定区域。我可以绘制整个南极洲,但这次我想“放大”并仔细观察该大陆的特定区域。

使用其他投影(Pyplot contour plot - clabel spacinghttp://matplotlib.org/basemap/api/basemap_api.htmlhttps://matplotlib.org/basemap/users/examples.html)的类似示例可在线获得,但我无法将这些示例应用于南极洲上的“spstere”投影。

我基本上想把我的地图集中在南极半岛地区,它大致跨越

llcrnrlon=-100,urcrnrlon=-30,llcrnrlat=-90,urcrnrlat=-55.0

我尝试将此代码与“spstere”项目一起使用,但 python 仅考虑 boundinglat 和 lon_0。我尝试更改 boundinglat 和 lon_0 的值,但它也不起作用。

知道我该怎么做吗?我也尝试过使用其他投影,例如“cyl”,但没有像“spstere”项目那样得到一个漂亮的正方形,而是得到一个水平矩形。

m = Basemap(projection='cyl',lon_0=0,lat_0=0,\
      llcrnrlon=-180,urcrnrlon=180,llcrnrlat=-90,urcrnrlat=-55.0,resolution='c')

任何帮助将不胜感激!

【问题讨论】:

    标签: python matplotlib coordinates projection


    【解决方案1】:

    使用极地立体投影'spstere',您可以使用例如boundinglat=-60:

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    
    m = Basemap(projection='spstere',boundinglat=-60,lon_0=180,resolution='c')
    m.drawcoastlines()
    
    plt.show()
    

    请注意,'spstere' 始终以南极为中心。
    为了获得不以南极为中心的地图,您需要使用"stere" 投影。为"stere" 投影设置角不是直截了当的。

    因此可以使用'spstere' 投影中的绘图并找到一些可以包围感兴趣区域的点。在这种情况下,例如

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import numpy as np
    
    m = Basemap(projection='spstere',boundinglat=-50,
                lon_0=180+(-100+-30)/2.,resolution='c')
    
    m.drawmeridians(np.arange(0,360,30),labels=[1,1,1,0])
    m.drawparallels(np.arange(-90,90,5))
    m.drawcoastlines()
    
    xll, yll = m(-150,-70) # <-- find those points by looking at meridians and parallels
    xur, yur = m(-30,-55)
    m.scatter([xll,xur], [yll, yur], c="crimson")
    plt.show()
    

    使用这些点 (-150,-70, -30,-55) 作为地图的角,然后您可以使用 'stere' 投影绘制地图。

    m = Basemap(projection='stere',resolution='c',
                lat_0=-90, lon_0=(-100+-30)/2.,  lat_ts=(-90.+-55.)/2.,
                llcrnrlon=-150,urcrnrlon=-30,llcrnrlat=-70,urcrnrlat=-55)
    

    如果不需要这种启发式方法,您可以通过在'spstere' 投影中创建一个虚拟地图来自动执行此过程,从相关矩形 (llcrnrlon=-100,urcrnrlon=-30,llcrnrlat=-90,urcrnrlat=-55.0) 计算坐标并在stere 投影中创建一个新底图跟他们。下面的函数取自ActiveState site(作者PG)。

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import numpy as np
    
    def polar_stere(lon_w, lon_e, lat_s, lat_n, **kwargs):
        '''Returns a Basemap object (NPS/SPS) focused in a region.
        lon_w, lon_e, lat_s, lat_n -- Graphic limits in geographical coordinates.
                                      W and S directions are negative.
        **kwargs -- Aditional arguments for Basemap object.
        '''
        lon_0 = lon_w + (lon_e - lon_w) / 2.
        ref = lat_s if abs(lat_s) > abs(lat_n) else lat_n
        lat_0 = np.copysign(90., ref)
        proj = 'npstere' if lat_0 > 0 else 'spstere'
        prj = Basemap(projection=proj, lon_0=lon_0, lat_0=lat_0,
                              boundinglat=0, resolution='c')
        lons = [lon_w, lon_e, lon_w, lon_e, lon_0, lon_0]
        lats = [lat_s, lat_s, lat_n, lat_n, lat_s, lat_n]
        x, y = prj(lons, lats)
        ll_lon, ll_lat = prj(min(x), min(y), inverse=True)
        ur_lon, ur_lat = prj(max(x), max(y), inverse=True)
        return Basemap(projection='stere', lat_0=lat_0, lon_0=lon_0,
                               llcrnrlon=ll_lon, llcrnrlat=ll_lat,
                               urcrnrlon=ur_lon, urcrnrlat=ur_lat, **kwargs)
    
    llcrnrlon=-100
    urcrnrlon=-30
    llcrnrlat=-90
    urcrnrlat=-55.0
    m = polar_stere(llcrnrlon, urcrnrlon, llcrnrlat, urcrnrlat)
    
    m.drawmeridians(np.arange(0,360,30),labels=[1,1,1,0])
    m.drawparallels(np.arange(-90,90,30),labels=[1,1,1,1])
    m.drawcoastlines()
    
    plt.show()
    

    【讨论】:

    • 太棒了。正是我需要的。感谢您的详细回答!
    • 很好的答案,很有帮助!
    猜你喜欢
    • 2019-01-07
    • 2017-07-03
    • 2018-12-10
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2021-12-14
    • 2021-01-23
    • 1970-01-01
    相关资源
    最近更新 更多