rio.plot.show是否使用彩色条?

2024-10-03 02:39:32 发布

您现在位置:Python中文网/ 问答频道 /正文

使用rio.plot.show后如何添加颜色条? 我尝试了很多方法,但都出现了各种错误

以下是我尝试的一种方法:

fig, ax = plt.subplots(figsize = (16, 16))

retted = rio.plot.show(ds, ax=ax, cmap='Greys_r')  

fig.colorbar(retted, ax=ax)
plt.title("Original")
plt.show()

这有错误:AttributeError: 'AxesSubplot' object has no attribute 'get_array'


Tags: 方法plot颜色show错误dsfigplt
2条回答

我同意这个解决方案,但我想补充一点,如果你和我一样,我通常有一个rasterio datasetreader对象(使用rasterio.open读取georeffed光栅数据的结果),而不仅仅是一个原始numpy数组。因此,对于RasterioV1.1.8,我必须执行额外的步骤,从datasetreader对象提取numpy数组。例如,对于单个波段:

dem = rasterio.open("GIS/anaPlotDEM.tif")
fig, ax = plt.subplots(figsize=(10,10))
image_hidden = ax.imshow(dem.read()[0])
fig.colorbar(image_hidden, ax=ax)
rasterio.plot.show(dem, ax=ax)

(我想加上这句话作为评论,但没有声誉点)

我做了上面david建议的事情,它成功了

fig, ax = plt.subplots(figsize=(5, 5))

# use imshow so that we have something to map the colorbar to
image_hidden = ax.imshow(image_data, 
                         cmap='Greys', 
                         vmin=-30, 
                         vmax=30)

# plot on the same axis with rio.plot.show
image = rio.plot.show(image_data, 
                      transform=src.transform, 
                      ax=ax, 
                      cmap='Greys', 
                      vmin=-30, 
                      vmax=30)

# add colorbar using the now hidden image
fig.colorbar(image_hidden, ax=ax)

相关问题 更多 >