如何以灰度转换造型瓷砖(2022)
我正在尝试将地形数据显示为背景,以将一些我最上面的洪水数据放置。我想以一定的颜色尺度(灰色似乎很受欢迎)以避免在洪水栏中避免使用这种颜色,以避免混淆。
我正在使用Cartopy的img_tiles,实际上已经提出了同样的问题(如何转换格雷斯尺度中的摄影瓷砖?)。但是现在,解决方案(添加CMAP =“ ...”)似乎在较新版本的Cartopy上不再起作用。尝试“ cmap”时,我有以下错误: error 。那么,是否有一种将图像转换为灰度绘制的方法?
我拥有的代码如下:
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')
如果没有cmap ='灰色',代码可行,但它会给我固定的颜色: 默认颜色
edit 1:我对ShadereliefeSri也有同样的问题从我如何获取地形数据,但从我的显示方式(ax)的创建方式或函数ax.Add_image()工作方式来看。当我添加cmap时,结果是相同的。代码如下:
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')
编辑2:试图用Spyder绘制,有趣的东西仍然显示出来,看起来不错,但是出现了错误,并且可以防止我使用savefig()。
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?). But now the solution (adding cmap="...") doesn't seem to work anymore on the newer version of Cartopy. I have the following error while trying "cmap":
error. 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
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. The result is the same when I add 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().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有运行您的代码的预选。我使用CATTOPY版本0.19.0.post1。
如果您更喜欢B/W图像对于背景,则可以从 ref 。例如,使用此代码段,
您可以获取此图:
I have no proplem running your code. I use cartopy version 0.19.0.post1.
If you prefer B/W image for the background, the "toner" maps are available to choose from ref. For example, with this code snippet
you can get this plot: