python – Unable to Plot Open Data Cube Dataset Image: How to Fix

jupyter notebookopen-data-cubepython

I'm trying to plot the indexed Open Data Cube dataset image through the Jupyter Notebook. I'm using this demo notebook after making a clone of this repository on my local machine.
The plot() method as shown below is not showing any image on output.

import datacube
dc = datacube.Datacube(app = 'my_app', config = 'C:/Users/user/datacube.conf')
ds = dc.load(product='LISS3_test1',
        longitude=(85.75, 86),
        latitude=(20.25, 20.5),
        output_crs='EPSG:4326',
        resolution=(-0.000225, 0.000225)
)
nir_image = ds.nir
nir_image.plot(cmap = "Greys")

The only output is <matplotlib.collections.QuadMesh at 0x364bf59e88> without any image.

type(nir_image) command gives xarray.core.dataarray.DataArray as output.

print(ds.nir) command gives following output,

<xarray.DataArray 'nir' (time: 1, latitude: 1112, longitude: 1112)>
array([[[82, 77, 78, ..., 81, 76, 73],
        [82, 76, 72, ..., 77, 77, 75],
        [87, 78, 72, ..., 75, 77, 79],
        ...,
        [85, 84, 82, ..., 69, 68, 71],
        [83, 80, 76, ..., 71, 74, 73],
        [79, 78, 79, ..., 71, 74, 77]]], dtype=uint8)
Coordinates:
  * time         (time) datetime64[ns] 2021-07-23T14:30:54.147622
  * latitude     (latitude) float64 20.5 20.5 20.5 20.5 ... 20.25 20.25 20.25
  * longitude    (longitude) float64 85.75 85.75 85.75 85.75 ... 86.0 86.0 86.0
    spatial_ref  int32 4326
Attributes:
    units:         1
    nodata:        0
    crs:           EPSG:4326
    grid_mapping:  spatial_ref

Can anyone please say what's wrong here and how to plot any particular band image in the notebook?

I am doing a local installation of Open Data Cube and indexed some datasets with PostgreSQL on a Windows 8.1 system.

Edit – 1 ——————————————————————

What I understood from other example notebooks is, the nir_image is 3 dimensional data with time being the 3rd dimension. So I did manage to plot with the following code.

nir_image = ds.nir.isel(time=0)
nir_image.plot(cmap="gray") 

This code displayed the image, but when I closed the system and open again, no image is being displayed. only <matplotlib.collections.QuadMesh at 0x170bcc5c48> is in the output.

What is the problem here? Why is it showing only some of the times?

Best Answer

I think this might be able to be solved just by running plt.show() after calling the .plot() method.

Another issue I have sometimes encountered is importing matplotlib and then plotting something in the same cell in Jupyter Notebooks. I can't remember why this issue was caused or if it fixed now, but perhaps the following will work (with the imports in a different cell):

import datacube
import matplotlib.pyplot as plt
dc = datacube.Datacube(app = 'my_app', config = 'C:/Users/user/datacube.conf')
ds = dc.load(product='LISS3_test1',
        longitude=(85.75, 86),
        latitude=(20.25, 20.5),
        output_crs='EPSG:4326',
        resolution=(-0.000225, 0.000225)
)
nir_image = ds.nir
nir_image.plot(cmap = "Greys")
plt.show()
Related Question