1

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?

1

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()
1

Jupyter Notebooks may need the matplotlib enable magic before it starts showing the output in a cell.

This is called the matplotlib inline backend.

Something like this in a cell should tell you if this is working.

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp');

See more details here : https://github.com/ipython/matplotlib-inline

1
  • This didn't solve my issue. With this, the image comes as some sort of line plot but I needed it to display the image. I managed to display the image as I have mentioned in the edit. But now again it's not showing.
    – Badal
    Aug 4 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.