【问题标题】:cartopy set extent with central_longitude=180cartopy 设置范围,central_longitude=180
【发布时间】:2020-01-03 19:28:17
【问题描述】:

Cartopy 0.17.0: 当我设置central_longitude时,我不知道如何设置确切提供的范围:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45), crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

这对纬度进行了正确的子集化: 这对经度进行了正确的子集化,但有额外的标签:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45))
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

这会正确设置纬度:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45), crs=projection)
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

【问题讨论】:

    标签: python dictionary matplotlib cartopy extent


    【解决方案1】:

    使用 Cartopy 绘制跨越世界日期线的地图并不像您发现的那样简单。它需要一些技巧才能让它正确。最重要的是必须在代码的所有部分中正确使用 CRS。

    代码:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    # cartopy-0.17.0 pyshp-2.1.0
    
    cm = 180
    proj = ccrs.PlateCarree(central_longitude=cm)
    fig = plt.figure(figsize=[5, 8])
    ax = fig.add_subplot(1, 1, 1, projection=proj)
    ax.coastlines()
    
    # original ax.set_extent((-120, 120, -45, 45)) ?
    # Need longitude extent from -60 to +60 on PlateCarree(central_longitude=180)
    minlon = -60 + cm
    maxlon = +60 + cm
    ax.set_extent([minlon, maxlon, -45, 45], ccrs.PlateCarree())
    ax.gridlines(draw_labels=True, crs=proj)
    plt.show()
    

    输出 plot1,在 PlateCarree(central_longitude=180) 中带有经度标签,这本身是自然的,但不是地理规范。

    如果你想在上图中有普通的地理经度标签,你不能简单地使用

    ax.gridlines(draw_labels=True, crs=PlateCarree())
    

    在代码中,如您所见。

    输出 plot2,带有普通地理经度标签

    这需要 ax.gridlines() 中的具体指令如下:

    ax.gridlines(draw_labels=False, crs=ccrs.PlateCarree(), xlocs=[120,140,160,180,200,220,240])
    ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), xlocs=[120,140,160,180,-160,-140,-120])
    

    希望这对所有读者有用。

    【讨论】:

      【解决方案2】:

      这取决于“class PlateCarree”属性(以及所使用的 CylindricalProjection 的属性)。 请参阅documentation。 经度值 180 是一个边界。

      如果设置范围 [120 180 ...] 或 [-120 180 ...] 则没有问题。

      我认为尝试其他投影是有意义的。

      【讨论】:

      • 我想看到整个太平洋从-45到45N,而不是那样分裂。
      • 我明白了。那么,也许您会选择其他投影?
      【解决方案3】:

      例如: 投影 = ccrs.LambertCylindrical(central_longitude=180)

      很遗憾,“无法标注 Lambert 圆柱网格线。目前仅支持 PlateCarree 网格线。

      【讨论】:

        猜你喜欢
        • 2021-12-05
        • 1970-01-01
        • 2019-10-18
        • 2018-02-12
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多