我有一些卫星图像数据想用 Cartopy 显示。我已成功遵循此处详述的图像示例。导致此代码:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure(figsize=(12, 12))
img_extent = (-77, -59, 9, 26)
ax = plt.axes(projection=ccrs.PlateCarree())
# image data coming from server, code not shown
ax.imshow(img, origin='upper', extent=img_extent)
ax.set_xmargin(0.05)
ax.set_ymargin(0.10)
# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7)
ax.text(-117, 33, 'San Diego')
ax.coastlines()
ax.gridlines()
plt.show()
此代码生成以下图像
我的问题是卫星图像数据不在 PlateCarree 投影中,而是在墨卡托投影中。
但是当我得到轴对象时
ax = plt.axes(projection=ccrs.Mercator())
我失去了海岸线。
我看到了这里报告的问题。但
ax.set_global()
结果如下图:
数据不存在,圣地亚哥位于错误的位置。纬度/经度范围也发生了变化。我究竟做错了什么?
讨论后更新
主要问题是我没有使用该transform_points
方法正确指定目标投影中的图像范围。imshow
正如菲尔建议的那样,我还必须具体说明该方法中的坐标参考系。这是正确的代码:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
proj = ccrs.Mercator()
fig = plt.figure(figsize=(12, 12))
extents = proj.transform_points(ccrs.Geodetic(),
np.array([-77, -59]),
np.array([9, 26]))
img_extents = (extents[0][0], extents[1][0], extents[0][6], extents[1][7] )
ax = plt.axes(projection=proj)
# image data coming from server, code not shown
ax.imshow(img, origin='upper', extent=img_extents,transform=proj)
ax.set_xmargin(0.05)
ax.set_ymargin(0.10)
# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())
ax.coastlines()
ax.gridlines()
plt.show()
产生这个正确的地理投影卫星图像: