简体   繁体   English

如何以灰度转换 Cartopy 瓦片 (2022)

[英]How to convert Cartopy tiles in gray scale (2022)

I'm trying to display topography data as a background to put some flooding data I have on top.我正在尝试将地形数据显示为背景,以将我拥有的一些洪水数据放在最上面。 And I want to display this topography in a certain color scale (gray seems to be popular) to avoid having that color in my color bar for the flooding data to avoid confusion.而且我想以特定的色标(灰色似乎很流行)显示此地形,以避免在我的颜色条中为洪水数据使用该颜色以避免混淆。

I'm using Cartopy's img_tiles, and this same problem has actually been asked 4 years ago ( How to convert Cartopy tiles in grayscale? ).我正在使用 Cartopy 的 img_tiles,实际上 4 年前就有人问过同样的问题( How to convert Cartopy tiles in grayscale? )。 But now the solution (adding cmap="...") doesn't seem to work anymore on the newer version of Cartopy.但是现在解决方案(添加 cmap="...")似乎不再适用于较新版本的 Cartopy。 I have the following error while trying "cmap": error .尝试“cmap”时出现以下错误:错误 So, is there a different way to convert the image into a gray scale for plotting?那么,是否有不同的方法将图像转换为灰度以进行绘图?

The code I have is as follow:我的代码如下:

import matplotlib.pyplot    as plt
import cartopy.crs          as ccrs
import cartopy.io.img_tiles as cimgt

fig = plt.figure(figsize=(10,7.5))
ax  = plt.axes(projection=ccrs.PlateCarree())

[lon0,lat0,lon1,lat1] = [102., 11., 107., 15.]
ax.set_extent([lon0, lon1, lat0, lat1])

stamen_terrain = cimgt.Stamen(desired_tile_form="L", style="terrain-background") 
ax.add_image(stamen_terrain, 8, cmap='gray')

Without cmap='gray', the code works, but it will give me a fixed color: default color如果没有 cmap='gray',代码可以工作,但它会给我一个固定的颜色:默认颜色

Edit 1: I have the same problem with ShadeReliefESRI, seems like the problem is not coming from how I acquire the topography data but from how I display it, either with how the axis (ax) was created or how the function ax.add_image() work.编辑 1:我对 ShadeReliefESRI 有同样的问题,似乎问题不是来自我如何获取地形数据,而是来自我如何显示它,轴 (ax) 的创建方式或 function ax.add_image( ) 工作。 The result is the same when I add cmap.当我添加 cmap 时,结果是一样的。 Codes are as below:代码如下:

from cartopy.io.img_tiles import GoogleTiles
class ShadedReliefESRI(GoogleTiles):
    # shaded relief
    def _image_url(self, tile):
        x, y, z = tile
        url = ('https://server.arcgisonline.com/ArcGIS/rest/services/' \
               'World_Shaded_Relief/MapServer/tile/{z}/{y}/{x}.jpg').format(
               z=z, y=y, x=x)
        return url

ax.add_image(ShadedReliefESRI(), 8, cmap ='gray')

Edit 2: Trying to plot with Spyder, funny thing, the figure still show up, looking good, but the error also appears and it prevents me from using savefig().编辑 2:用 Spyder 尝试 plot,有趣的是,图形仍然显示,看起来不错,但错误也出现了,它阻止我使用 savefig()。

I have no proplem running your code.我没有问题运行你的代码。 I use cartopy version 0.19.0.post1.我使用 cartopy 版本 0.19.0.post1。

灰色背景

If you prefer B/W image for the background, the "toner" maps are available to choose from ref .如果您更喜欢背景的黑白图像,可以从ref中选择“碳粉”贴图。 For example, with this code snippet例如,使用此代码片段

stamen_terrain = cimgt.Stamen(desired_tile_form='RGB', style="toner")
ax.add_image(stamen_terrain, 8, alpha=0.4)

you can get this plot:你可以得到这个 plot:

灰色2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM