Python Display GeoTIFF File – How to Display GeoTIFF File in Python Using Rasterio and Matplotlib

geotiff-tiffmatplotlibpythonrasterrasterio

I have a list of GeoTIFF files, which I uploaded to a repo in Github. The files are generated in R, there I can read them with the raster library :

> library(raster)
Loading required package: sp 
> r = (raster::raster("./2020-07-24_B.tif"))
class      : RasterLayer 
dimensions : 36, 28, 1008  (nrow, ncol, ncell)
resolution : 10, 10  (x, y)
extent     : 680460, 680740, 4183700, 4184060  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=30 +datum=WGS84 +units=m +no_defs 
source     : 2020-07-24_B.tif 
names      : X2020.07.24_B 
values     : 687, 1494  (min, max)

raster::plot(r)

enter image description here

The files load nicely in QGIS:

enter image description here

But I am unable to make them work in Python:

import rasterio as rs
from matplotlib import pyplot
file = "B/2020-07-24_B.tif"
raster = rs.open(file)
array = raster.read()
pyplot.imshow(array[0])

enter image description here

I followed this tutorial.

At this point I wonder if the file could be corrupted in some way or is just an issue related with the tweaking of imshow params.

Best Answer

Yes. It is just an issue related with the parameters. Without vmin and vmax, matplotlib uses nodata value as minimum value, and stretches the color map.

enter image description here

So, you need to specify min and max value:

pyplot.imshow(array[0], vmin=687, vmax=array[0].max())

enter image description here

Instead of pyplot.imshow, you can use rasterio show method.

from rasterio.plot import show
show(raster)

enter image description here

Related Question