Reading and plotting GeoTiff with Xarray

Reading and plotting GeoTiff with Xarray#

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import matplotlib.colors as mcolors
dset = xr.open_dataset("srtm_41_19.tif", engine="rasterio")
dset
<xarray.Dataset>
Dimensions:      (band: 1, x: 837, y: 661)
Coordinates:
  * band         (band) int64 1
  * x            (x) float64 9.695e+05 9.696e+05 ... 1.038e+06 1.038e+06
  * y            (y) float64 6.25e+06 6.25e+06 6.25e+06 ... 6.196e+06 6.196e+06
    spatial_ref  int64 ...
Data variables:
    band_data    (band, y, x) float32 ...

Visualize (default QGIS)#

dset["band_data"].plot(cmap="Greys_r")
<matplotlib.collections.QuadMesh at 0x7f1f16358250>
_images/8a1f61123a65e46ef263efdf35ae366d2786bde9bdcc3141c30183868edf8687.png

Visualize with pseudo colour#

rgb_colors = np.array([[  0.,  80., 191.],
 [ 42., 152. ,  8.],
 [255. ,248. , 57.],
 [255. ,166. ,  0.],
 [189. , 11.,  35.]])

rgb_colors = rgb_colors/255.
my_colormap = mcolors.LinearSegmentedColormap.from_list('colormap', rgb_colors)
dset["band_data"].plot(cmap=my_colormap)
<matplotlib.collections.QuadMesh at 0x7f1f163d3bd0>
_images/99358a39786538d3e82504a866843cbe5ec2e67a4c6fa6c99d687356933e99a4.png