Python 根据绘图的投影调整cartopy中的坐标

Python 根据绘图的投影调整cartopy中的坐标,python,python-2.7,cartopy,Python,Python 2.7,Cartopy,在以下示例中,如果我使用ccrs.Mercator()投影而不是ccrs.PlateCarree(),我将失去我的分数(即,我不理解坐标的变化): 看起来我的点的坐标从(6,56)到(0,0) 我错过了什么? 为什么ccrs.PlateCarree()的行为正确,而ccrs.Mercator()的行为不正确?我应该在某处添加任何变换吗 [使用解决方案编辑] 我最初的困惑来自这样一个事实,即projection适用于绘图,而transform适用于数据,这意味着当它们不共享同一个系统时,它们应

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

看起来我的点的坐标从(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()

是的,您应该将
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文档中提供了有关变换和投影的基本指南。为避免意外,在地图上绘制数据时,应始终指定变换。

谢谢-好的,实际上这里的关键点是
投影
确定绘图的投影,而
变换
确定数据的系统,这意味着使用
投影=ccrs.Mercator()的绘图
如果数据在lon-lat坐标中,则仍应与
transform=ccrs.PlateCarree()一起使用。您的示例并没有解决我最初的问题,因为它用
ccrs.PlateCarree()
解决了这个问题,顺便说一句,它仍然是正确的,因为这是数据系统。我建议将答案修改为更棘手的“ccrs.Mercator()”案例。。。
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)