根据绘图的投影调整纸箱中的坐标

问题描述 投票:1回答:1

在下面的例子中,如果我使用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()

enter image description here

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


[使用解决方案编辑]

我最初的混淆来自于projection适用于情节,而transform适用于数据,这意味着当他们不共享同一系统时应该设置不同 - 我第一次尝试transform哪里错误,如下面的ax1ax1bis是解决方案。

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()

enter image description here

python python-2.7 cartopy
1个回答
3
投票

是的,您应该将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)

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

© www.soinside.com 2019 - 2024. All rights reserved.