ras*_*n58 6 python plot matplotlib 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()
Run Code Online (Sandbox Code Playgroud)
这有错误: AttributeError: 'AxesSubplot' object has no attribute 'get_array'
小智 7
我做了大卫上面的建议,它奏效了!
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)
Run Code Online (Sandbox Code Playgroud)
小智 6
plt.imshow() 和 rasterio.plot.show() 返回不同的对象。plt.colorbar() 需要一个可映射对象,因此会感到困惑。因为光栅绘图是 matplotlib 的包装器,所以我认为最直接的方法是提供 maptlotlib 期望的底层对象。
retted = rio.plot.show(ds, ax=ax, cmap='Greys_r')
im = retted.get_images()[0]
fig.colorbar(im, ax=ax)
Run Code Online (Sandbox Code Playgroud)