从底图近侧透视 (nsper) 投影迁移到使用 cartopy 地球静止投影

Migrating from basemap Near Sided Perspective (nsper) projection to using cartopy Geostationary projection

我正在尝试将一些代码从底图迁移到 cartopy,我很困惑为什么我不能让它工作。但是,我基本上想在底图提供的类似 'nsper' 投影上绘制散点图。

原始代码如下所示:

from mpl_toolkits.basemap import Basemap
import numpy as np
from matplotlib import pyplot as  plt
lon_0 = -75.0
image_name = 'original.png'
lon_swath1 = np.arange(-65,66,1)
lat_swath1 = np.zeros(np.shape(lon_swath1))

lat_swath2 = np.arange(-40,40,1)
lon_swath2 = -75.0*np.ones(np.shape(lat_swath2))

m = Basemap(projection='nsper',lon_0=lon_0,lat_0=0)

x1,y1 = m(lon_swath1,lat_swath1)
x2,y2 = m(lon_swath2,lat_swath2)

fig = plt.figure(figsize=(10,6.5))
m.drawcoastlines()
m.scatter(x1, y1, s=2.5, marker="o")
m.scatter(x2, y2, s=2.5, marker="x")
fig.savefig('%s' % image_name, format='png')
plt.show()
plt.close(fig)

并生成如下图: original basemap figure.

如果我尝试做我认为在 cartopy 中等效的事情,我就得不到完整的地球仪,而且无论我多么努力都做不到

import cartopy.crs as ccrs
import numpy as np
from matplotlib import pyplot as  plt
lon_0 = -75.0
image_name = 'test.png'
lon_swath1 = np.arange(-65,66,1)
lat_swath1 = np.zeros(np.shape(lon_swath1))

lat_swath2 = np.arange(-40,40,1)
lon_swath2 = -75.0*np.ones(np.shape(lat_swath2))

fig = plt.figure(figsize=(10,6.5))
ax = plt.axes(projection= ccrs.Geostationary(central_longitude=lon_0))
ax.coastlines()
plt.scatter(lon_swath1, lat_swath1, s=2.5, marker="o", transform=ccrs.Geodetic())
plt.scatter(lon_swath2, lat_swath2, s=2.5, marker="x", transform=ccrs.Geodetic())
fig.savefig('%s' % image_name, format='png')
plt.show()
plt.close(fig)

zoomed in Geostationary equivalent

我想要的是一个完整的地球范围,但是,如果我试图强制执行以下问题,则会出现错误。

import cartopy.crs as ccrs
import numpy as np
from matplotlib import pyplot as  plt
lon_0 = -75.0
image_name = 'broken.png'
lon_swath1 = np.arange(-65,66,1)
lat_swath1 = np.zeros(np.shape(lon_swath1))

lat_swath2 = np.arange(-40,40,1)
lon_swath2 = -75.0*np.ones(np.shape(lat_swath2))

fig = plt.figure(figsize=(10,6.5))
ax = plt.axes(projection= ccrs.Geostationary(central_longitude=lon_0))
extent = ax.get_extent()
ax.coastlines()
plt.scatter(lon_swath1, lat_swath1, s=2.5, marker="o", transform=ccrs.Geodetic())
plt.scatter(lon_swath2, lat_swath2, s=2.5, marker="x", transform=ccrs.Geodetic())
ax.set_extent(extent,crs=ccrs.Geostationary(central_longitude=lon_0))
fig.savefig('%s' % image_name, format='png')
plt.show()
plt.close(fig)

我得到的错误:

    ax.set_extent(extent,crs=ccrs.Geostationary(central_longitude=lon_0))
      File "/users/karpowicz/.local/external/anaconda/lib/python2.7/site-packages/cartopy/mpl/geoaxes.py", line 652, in set_extent
        ylim=self.projection.y_limits))
ValueError: Failed to determine the required bounds in projection coordinates. Check that the values provided are within the valid range (x_limits=[-5372584.78444, 5372584.78444], y_limits=[-5372584.78444, 5372584.78444]).

如果有人有任何建议或想法,我将不胜感激!

这里的问题是您的地图在投影中,但您试图通过的范围却不是。老实说,我已经忘记了具体的修复方法是什么(我之前 运行 已经深入了解了),但这里有一个快捷方式——扔掉 ax.set_extent 并改用它:

ax.set_global()

我觉得这像是一个错误。 @ResMar 关于范围不在投影中的断言是不正确的。根据 the documentation for get_extent():

If no crs is given, the returned extents' coordinate system will be the CRS of this Axes.

即使该函数返回的范围超出错误消息中指定的 x 和 y 限制,也可以提供在这些限制内但仍会产生错误的范围。

编辑:@ResMar 说 ax.set_global() 是这种情况下的正确方法是正确的