Cartopy理解变换和投影关键字

翻译一下官网说明,为了帮助和我一样英语不好的人。
Understanding the transform and projection keywords — cartopy 0.20.0 documentation

很容易混淆投影和变换关键字参数的实际含义。在这里,我们将使用一些简单的例子来说明每种方法的效果。

核心概念是子图(或轴 zxes)的投影独立于定义数据的坐标系。“投影”参数用于创建绘图并确定结果绘图的投影(即绘图的外观)。plotting函数transform参数告诉Cartopy数据在哪个坐标系中定义。

首先,我们将创建一些在常规纬度/经度网格上定义的虚拟数据:

import numpy as np

lon = np.linspace(-80, 80, 25)
lat = np.linspace(30, 70, 25)
lon2d, lat2d = np.meshgrid(lon, lat)

data = np.cos(np.deg2rad(lat2d) * 4) + np.sin(np.deg2rad(lon2d) * 4)

让我们尝试在PlateCarree投影中绘制一个图,而不指定transform参数。由于数据恰好是在我们正在绘制的同一坐标系中定义的,因此这实际上是正确的:

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

# The projection keyword determines how the plot will look
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()

ax.contourf(lon, lat, data)  # didn't use transform, but looks ok...
plt.show()

 下面我们加入 transform关键字:

# The data are defined in lat/lon coordinate system, so PlateCarree()
# is the appropriate choice:
data_crs = ccrs.PlateCarree()

# The projection keyword determines how the plot will look
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()

ax.contourf(lon, lat, data, transform=data_crs)
plt.show()

 

 看到画的图没有改变吗?这是因为未提供transform参数时的默认假设是坐标系与投影匹配,到目前为止一直如此。

现在,我们将再次尝试此操作,但对绘图使用不同的投影。我们将绘制到旋转的极点投影上,我们将省略transform参数以查看发生了什么:

# Now we plot a rotated pole projection
projection = ccrs.RotatedPole(pole_longitude=-177.5, pole_latitude=37.5)
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=projection)
ax.set_global()
ax.coastlines()

ax.contourf(lon, lat, data)  # didn't use transform, uh oh!
plt.show()

 结果图不正确!我们没有告诉Cartopy我们的数据是在什么坐标系中定义的,所以它假设它与我们正在绘制的投影相同,并且数据绘制在错误的位置。

我们可以通过提供transform参数来解决此问题,该参数与以前相同,因为数据的坐标系没有更改:

# A rotated pole projection again...
projection = ccrs.RotatedPole(pole_longitude=-177.5, pole_latitude=37.5)
data_crs = ccrs.PlateCarree()
plt.figure(figsize=(6, 3))
ax = plt.axes(projection=projection)
ax.set_global()
ax.coastlines()

# ...but now using the transform argument
ax.contourf(lon, lat, data, transform=data_crs)
plt.show()

最安全的做法是始终提供transform关键字,而不考虑使用的投影,并避免让Cartopy对数据的坐标系进行假设。这样做允许您为绘图选择任何地图投影,并允许Cartopy在数据应位于的位置绘制数据:

# We can choose any projection we like...
projection = ccrs.InterruptedGoodeHomolosine()
plt.figure(figsize=(6, 7))
ax1 = plt.subplot(211, projection=projection)
ax1.set_global()
ax1.coastlines()
ax2 = plt.subplot(212, projection=ccrs.NorthPolarStereo())
ax2.set_extent([-180, 180, 20, 90], crs=ccrs.PlateCarree())
ax2.coastlines()

# ...as long as we provide the correct transform, the plot will be correct
ax1.contourf(lon, lat, data, transform=data_crs)
ax2.contourf(lon, lat, data, transform=data_crs)
plt.show()

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cartopy 是一个 Python 库,提供了地图制图工具和地理空间数据处理功能。其中包括许多投影方式,兰博托投影(Lambert Conformal Projection)是其中之一。下面是一个简单的示例,展示如何使用 Cartopy 在兰博托投影下绘制地图。 首先,需要安装 Cartopy 库。可以使用 pip 命令进行安装,命令如下: ``` pip install cartopy ``` 接下来,导入必要的库和模块: ```python import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature ``` 然后,创建一个兰博托投影: ```python proj = ccrs.LambertConformal(central_longitude=120, central_latitude=35) ``` 这里的 `central_longitude` 和 `central_latitude` 分别指定了中心经度和中心纬度,可以根据需要进行修改。 接着,创建一个地图对象,并设置投影方式为兰博托投影: ```python fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(1, 1, 1, projection=proj) ``` 然后,添加一些地图特征,例如陆地、海洋和国界等: ```python ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.BORDERS) ``` 最后,可以绘制一些数据,例如气象数据、地形数据等。下面是一个简单的示例,绘制了一个简单的圆形: ```python import numpy as np theta = np.linspace(0, 2*np.pi, 100) radius = 30 x = radius * np.cos(theta) + 120 y = radius * np.sin(theta) + 35 ax.plot(x, y, transform=ccrs.PlateCarree(), color='red') ``` 这里的 `transform` 参数指定了数据的坐标系,这里使用的是等经纬度投影。 完整的代码如下所示: ```python import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature import numpy as np proj = ccrs.LambertConformal(central_longitude=120, central_latitude=35) fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(1, 1, 1, projection=proj) ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.BORDERS) theta = np.linspace(0, 2*np.pi, 100) radius = 30 x = radius * np.cos(theta) + 120 y = radius * np.sin(theta) + 35 ax.plot(x, y, transform=ccrs.PlateCarree(), color='red') plt.show() ``` 运行代码后,可以得到一个带有圆形的兰博托投影地图。可以根据需要修改代码,添加更多的地图特征和数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值