1

我正在尝试将一些代码从底图迁移到 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)

并生成一个看起来像这样的图形: 原始底图图形。

如果我尝试做我认为与 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)

放大地球静止等效

我想要的是一个完整的全球范围,但是,如果我尝试使用以下内容强制解决问题,我会收到错误消息。

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]).

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

4

2 回答 2

1

The issue here is that your map is in a projection but the extent you're trying to pass is not. I've honestly forgotten what the specific fix for this is (I've run into it quite a bit before), but here's a shortcut—throw out the ax.set_extent and use this instead:

ax.set_global()
于 2017-02-03T06:13:31.323 回答
1

This looks like a bug to me. @ResMar's assertion that the extents aren't in a projection is incorrect. According to the documentation for get_extent():

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

Even if the extents returned by that function are outside the x and y limits specified in the error message, it's possible to provide extents which are within those limits but still produce the error.

Edit: @ResMar is correct in saying that ax.set_global() is the right way to go in this case

于 2017-02-03T11:51:51.710 回答