Python – Customizing Axis and Labels of Rasterio Plots

matplotlibpythonrasterio

I have three plots consisting in a vector polygon over three different raster layers arranged in facets.
I used rasterio, geopandas and matplotlib.
I would modify the labels of axis in order to show the coordinates in decimal degrees instead of the rows/columns number.
And how to delete any axis labels?

import numpy as np
import rasterio 
from rasterio import plot
import geopandas as gpd
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize']=9,4

# Read raster

b1 = rasterio.open()
b2 = rasterio.open()
b3 = rasterio.open()

ba1 = b1.read()
ba2 = b2.read()
ba3 = b3.read()

bres1 = np.squeeze(ba1)
bres2 = np.squeeze(ba2)
bres3 = np.squeeze(ba3)

# Read geometry

shapefile = gpd.read_file()

# Plot raster colorbar

fig, ax = plt.subplots(nrows=2,ncols=2, figsize=(12,12))

base1 = plt.imshow(bres1)
base2 = plt.imshow(bres2)
base3 = plt.imshow(bres3)

image1  = rasterio.plot.show(b1, ax=ax[0,0], cmap='nipy_spectral', title='Band 1')
image2  = rasterio.plot.show(b2, ax=ax[0,1], cmap='nipy_spectral', title='Band 2')
image3  = rasterio.plot.show(b3, ax=ax[1,0], cmap='nipy_spectral', title='Band 3')

shapefile.plot(ax=image1, color='red')
shapefile.plot(ax=image2, color='red')
shapefile.plot(ax=image3, color='red')

fig.colorbar(base1, ax=ax[0,0])
fig.colorbar(base2, ax=ax[0,1])
fig.colorbar(base3, ax=ax[1,0])

fig.delaxes(ax=ax[1,1]) 
plt.show()

Best Answer

fig, ax = plt.subplots(nrows=2,ncols=2, figsize=(12,12))

base1 = plt.imshow(bres1)
base2 = plt.imshow(bres2)
base3 = plt.imshow(bres3)

image1  = rasterio.plot.show(b1, ax=ax[0,0], cmap='nipy_spectral', title='Band 1')
image2  = rasterio.plot.show(b2, ax=ax[0,1], cmap='nipy_spectral', title='Band 2')
image3  = rasterio.plot.show(b3, ax=ax[1,0], cmap='nipy_spectral', title='Band 3')

shapefile.plot(ax=image1, color='red')
shapefile.plot(ax=image2, color='red')
shapefile.plot(ax=image3, color='red')

fig.colorbar(base1, ax=ax[0,0])
fig.colorbar(base2, ax=ax[0,1])
fig.colorbar(base3, ax=ax[1,0])

fig.delaxes(ax=ax[1,1]) 

[ax.set_axis_off() for ax in ax.ravel()]  # Just need to add this line

plt.show()