Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Cartopy在地图上显示图像时的投影问题_Python_Matplotlib_Cartopy - Fatal编程技术网

Python 使用Cartopy在地图上显示图像时的投影问题

Python 使用Cartopy在地图上显示图像时的投影问题,python,matplotlib,cartopy,Python,Matplotlib,Cartopy,我有一些卫星图像数据,我想用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 dat

我有一些卫星图像数据,我想用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投影中,而是墨卡托投影

但是当我得到axis对象时

ax = plt.axes(projection=ccrs.Mercator())
我失去了海岸线

我看到了这个问题的报道。但是

此图像中的结果:

数据不存在,圣地亚哥的位置错误。此外,lat/lon范围也已更改。我做错了什么

讨论后更新

主要问题是,我没有使用
transform\u 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() 
生成正确的地球投影卫星图像:


理想情况下,在使用cartopy(通过transform关键字)打印时,尽量明确数据所在的坐标参考系。这意味着您可以在脚本中切换投影,数据将自动放置在正确的位置

因此,在您的例子中,
plt.imshow
应该有一个
transform=ccrs.Mercator()
关键字参数(您可能需要一个更具体的参数化Mercator实例)。如果您的范围是大地坐标系(lats和lons),则必须将边界框转换为墨卡托坐标,但除此之外,其他一切都应按预期工作

注意:我将继续更新以包含转换参数;-)(公关部:)


HTH

我的Cartopy 0.7在升级到Mpl 1.3后坏了,所以我无法为您测试。但是,您不应该指定绘图数据的坐标吗?如果你没有,我认为坐标被假定为等于轴的投影。因此,请尝试将
transform=ccrs.PlateCarree()
添加到绘图命令中。谢谢,我添加了带有正确代码的讨论后更新。太好了。很高兴这有帮助。好情节:-)
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()