【问题标题】:Adapting coordinates in cartopy depending on the projection of a plot根据绘图的投影调整 cartopy 中的坐标
【发布时间】:2018-03-01 16:03:25
【问题描述】:

在以下示例中,如果我使用ccrs.Mercator() 投影而不是ccrs.PlateCarree(),我将失去我的观点(即,我不理解坐标的变化):

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

mypt = (6, 56)

ax0 = plt.subplot(221, projection=ccrs.PlateCarree()) # OK
ax1 = plt.subplot(222, projection=ccrs.Mercator())    # NOT OK
ax2 = plt.subplot(224, projection=ccrs.Mercator())    # NOT OK

def plotpt(ax, extent=(-15,15,46,62)):
    ax.plot(mypt[0], mypt[1], 'r*', ms=20)
    ax.set_extent(extent)
    ax.coastlines(resolution='50m')
    ax.gridlines(draw_labels=True)

plotpt(ax0)
plotpt(ax1)
plotpt(ax2, extent=(-89,89,-89,89))

plt.show()

看起来我点的坐标从 (6,56) 变为 (0,0) 我错过了什么? 为什么ccrs.PlateCarree() 的行为正确,ccrs.Mercator() 的行为不正确?我应该在某处添加任何转换吗?


[编辑解决方案]

我最初的困惑来自projection 适用于情节,而transform 适用于数据,这意味着当它们不共享同一个系统时它们应该设置不同 - 我第一次尝试transform在下面的ax1 中出错的地方,ax1bis 是解决方案。

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

mypt = (6, 56)

ax0 = plt.subplot(221, projection=ccrs.PlateCarree())
ax1 = plt.subplot(222, projection=ccrs.Mercator())
ax1bis = plt.subplot(223, projection=ccrs.Mercator())
ax2 = plt.subplot(224, projection=ccrs.Mercator())

def plotpt(ax, extent=(-15,15,46,62), **kwargs):
    ax.plot(mypt[0], mypt[1], 'r*', ms=20, **kwargs)
    ax.set_extent(extent)
    ax.coastlines(resolution='50m')
    ax.gridlines(draw_labels=True)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

plotpt(ax0) # correct because projection and data share the same system
plotpt(ax1, transform=ccrs.Mercator()) # WRONG
plotpt(ax1bis, transform=ccrs.PlateCarree()) # Correct, projection and transform are different!
plotpt(ax2, extent=(-89,89,-89,89), transform=ccrs.Mercator()) # WRONG

plt.show()

【问题讨论】:

    标签: python python-2.7 cartopy


    【解决方案1】:

    是的,您应该将 transform 关键字添加到 plot 调用中。您还应该指定要设置范围的坐标系:

    def plotpt(ax, extent=(-15,15,46,62)):
        ax.plot(mypt[0], mypt[1], 'r*', ms=20, transform=ccrs.PlateCarree())
        ax.set_extent(extent, crs=ccrs.PlateCarree())
        ax.coastlines(resolution='50m')
        ax.gridlines(draw_labels=True)
    

    现在可以在 cartopy 文档http://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html 中找到有关变换和投影的基本指南。为避免意外,您应该始终在地图上绘制数据时指定转换。

    【讨论】:

    • 谢谢 - 好的,实际上这里的关键点是projection 确定了绘图的投影,而transform 确定了数据的系统,这意味着带有projection=ccrs.Mercator() 的绘图仍然应该是如果数据在经纬度坐标中,则与 transform=ccrs.PlateCarree() 一起使用。您的示例没有解决我最初的问题,因为它解决了ccrs.PlateCarree() 的情况,这仍然是偶然正确的,因为这是数据系统。我建议为更棘手的“ccrs.Mercator()”案例调整答案......
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2017-04-24
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多