python - rio.plot.show 带颜色条?

标签 python matplotlib plot rasterio

使用 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'

最佳答案

我做了什么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)

关于python - rio.plot.show 带颜色条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61327088/

相关文章:

r - R绘制所有轴标签(防止被跳过)

python - 如何避免在 numpy 中基于数组的函数内迭代?

python - py2exe 可执行文件以看似随机的方式崩溃

python - 如何在 Keras 中使用 ImageDataGenerator 调整标签?

python - Matplotlib:使 x 轴更长

python - 在所有子图中绘制带有列的 DataFrame

python - 使用全息 View 设置 x 和 y 标签

python - 选择其子/孙/..包含具有指定模式的元素的元素

python - 在 matplotlib 中绘制 csv 文件

r - 在R中使用graph.adjacency()