我对用 Cartopy 渲染谷歌图块有点困惑。与标准的谷歌地图相比,该地图看起来非常糟糕。
示例(代码来自https://ocefpaf.github.io/python4oceanographers/blog/2015/06/22/osm/):
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
def make_map(projection=ccrs.PlateCarree()):
fig, ax = plt.subplots(figsize=(9, 13),
subplot_kw=dict(projection=projection))
gl = ax.gridlines(draw_labels=True)
gl.xlabels_top = gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
return fig, ax
import cartopy.io.img_tiles as cimgt
extent = [-39, -38.25, -13.25, -12.5]
request = cimgt.GoogleTiles()
fig, ax = make_map(projection=request.crs)
ax.set_extent(extent)
ax.add_image(request, 10)
生成:
与链接网站上显示的相同图像相比,看起来非常糟糕 - 看看文本标签和街道号码的像素化渲染:
更改缩放级别似乎并不能改善情况。
这是我正在处理的地图上的另一个示例,由 Cartopy 和 googletiles() 渲染:
中显示的相同地图有人知道这个奇怪问题的原因是什么以及如何解决吗?
这个问题也在 cartopy 问题跟踪器上被问到,网址为 https://github.com/SciTools/cartopy/issues/1048,建议设置
interpolation=
关键字参数。这是 imshow 的标准 matplotlib 插值,记录在 https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html。
我们在问题跟踪器中确定,您在这里看到的是
nearest
的插值。将其更改为 bilinear
会得到很好的结果,并且使用不同的插值方案可以获得更好的结果。例如,spline36
方案会产生非常令人愉快的图像......
因此,您的示例代码为:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.img_tiles as cimgt
extent = [-39, -38.25, -13.25, -12.5]
request = cimgt.OSM()
fig = plt.figure(figsize=(9, 13))
ax = plt.axes(projection=request.crs)
gl = ax.gridlines(draw_labels=True, alpha=0.2)
gl.xlabels_top = gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
ax.set_extent(extent)
ax.add_image(request, 10)
plt.show()
我们得到:
要设置
bilinear
插值,我们可以将 add_image
行更改为:
ax.add_image(request, 10, interpolation='bilinear')
更好的是,让我们尝试一下 spline36 之类的东西:
ax.add_image(request, 10, interpolation='spline36')
将这些图像并排放置:
对于在非本机投影上绘制图块的情况,有一个警告(如https://github.com/SciTools/cartopy/issues/1048#issuecomment-417001744中指出)。在这种情况下,我们有两个变量需要配置:
接受的答案中有一个小错字。
ax.add_image(request, 10, interpolation='spine36')
应该是
ax.add_image(request, 10, interpolation='spline36')
我对 cartopy 版本 0.22.0 也有同样的问题 改变插值只能稍微改善图像质量。 继这篇文章https://github.com/SciTools/cartopy/issues/1048#issuecomment-417001744 我还尝试使用关键字
regrid_shape
更改重新网格的分辨率
但在这个版本中这个关键字不存在!
我的代码如下:
imagery = OSM()
plt.figure(figsize=(6,6), dpi=200)
ax = plt.axes(projection=imagery.crs)
ax.set_extent((Leiden_coords[1]-0.02, Leiden_coords[1]+0.02, Leiden_coords[0]-0.01, Leiden_coords[0]+0.01))
ax.add_image(imagery, 15, interpolation='spline36', regrid_shape=1000)
plt.savefig('Leiden_map_test_osm_default.png')
plt.show()
错误消息的最后几行是:
AttributeError: AxesImage.set() got an unexpected keyword argument 'regrid_shape'
如何提高地图图像的质量?