【问题标题】:How to transpose from cartopy to axes coords如何从 cartopy 转置轴坐标
【发布时间】:2021-09-19 14:30:35
【问题描述】:

我有一个带有一些点的 cartopy GeoAxesSubplot,可能还有线或多边形。投影可以是 cartopy 支持的任何投影,包括正交投影。

我可以使用不同的转换进行绘图,如 here 所述:

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

# Projection could be any, e.g. globe or Arctic Stereopolar...
ax = plt.axes(projection=ccrs.Mollweide())
ax.coastlines()

# Plot using the coordinate system of the Axes
a = ax.plot(0.45, 0.5, transform=ax.transAxes, marker='o', ms=10)

# Plot using the projected coordinates using cartopy.crs
b = ax.plot(0, 0, transform=ccrs.PlateCarree() , marker='o', ms=10)

我想转换地理坐标以获取对象在轴上的笛卡尔坐标(例如子图)。即图中坐标轴[0,1]范围内的坐标,(0,0)在左下角,(1,1)在右上角。

在上述情况下,b 应转换为 (0.5, 0, 5),因为它位于地图的中心。

可以使用transform_points 完成类似的操作,但是我无法转置为axes-coords

在 matplotlib 和 cartopy 中定义了许多参数来控制对象在地图上的位置(范围、投影、中心子午线、视图高度等)。因此,引入另一个库可能会很尴尬。


给出的答案,例如here 解释了如何实现反向,但是,该示例没有给出如何生成轴坐标的正确答案。

【问题讨论】:

    标签: python matplotlib transform cartopy


    【解决方案1】:

    请记住,“地理坐标”的定义并不那么明确,因为您正在混合两个投影(Mollweide 和 PlateCarree),两者都使用“地理坐标”。还要小心使用精确的中心,因为即使您使用了不正确的坐标,它也可能会意外地看起来正确。

    因此您可能首先需要将您的数据转换为地图的投影(投影)。

    除了您链接到的 Matplotlib 转换教程之外,还提供了进行转换所需的所有信息。

    设置输入:

    from matplotlib import pyplot as plt
    import cartopy.crs as ccrs
    
    # sample point coordinates in Plate-Carree
    x_pc = -110.0 # longitude
    y_pc = 45.0 # latitude
    
    map_proj = ccrs.Mollweide()
    data_proj = ccrs.PlateCarree()
    

    转换取决于轴的xlimylim,因此首先设置使用ax.set_global() 很重要。这给出了从投影到显示坐标(以及后续轴坐标)的正确映射。

    fig, ax = plt.subplots(subplot_kw=dict(projection=map_proj), facecolor='w')
    ax.set_global()
    ax.coastlines()
    
    b = ax.plot(x_pc, y_pc, 'go', transform=data_proj, ms=5)
    
    # convert to map-coordinates (Mollweide)
    x_mollw, y_mollw = ax.projection.transform_point(x_pc, y_pc, data_proj)
    
    # convert to display coordinates
    x_disp, y_disp = ax.transData.transform((x_mollw, y_mollw))
    
    # convert to axes coordinates
    x_axes, y_axes = ax.transAxes.inverted().transform((x_disp, y_disp))
    
    # plot same point but using axes coordinates
    ax.plot(x_axes, y_axes, 'ro', transform=ax.transAxes, ms=10, mfc='none', mew=2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多