0

I want to plot multiple hyperspectral images with sp.imshow. I know this returns a R,G,B visualization. I have 13 HSI files (13 .hdr and 13 .img files). I know how to plot and analyze individual files but I want an overview of all my samples in a grid.

I am also aware of creating the fig, axes previously. Yet subplots are still confusing. This is what I have so far.

from pathlib import Path
import spectral as sp
import matplotlib.pyplot as plt

files_path = Path(r"C:\data\Reflectance_Calibrated")
hdr_list = list(files_path.glob('*.hdr'))
bin_list = list(files_path.glob('*.img'))

targets = list(zip(hdr_list,bin_list))

i = 0

## Here is where I tried doing a for loop, yet it did not work.

for k, target in enumerate(targets):
    target_open = sp.envi.open(targets[i][0], targets[i][1])
    sp.imshow(target_open)
    i += 1

I am looking for something like sp.imshow(target_open).add_subplot(ax)

Has anyone tried doing subplots with spectral.imshow objects?

Any help would be appreciated.

1 Answers1

0

There are a few options to achieve what you want. One is to use plt.subplot to select each grid cell, then when you call sp.imshow, pass the fignum keyword. For example, to create an Nx1 grid of images (i.e., a grid with a single column):

fig = plt.figure()
for k, target in enumerate(targets):
    target_open = sp.envi.open(targets[k][0], targets[k][1])
    plt.subplot(len(targets), 1, k + 1)
    sp.imshow(target_open, fignum=fig.number)

Another option is to use sp.get_rgb to retrieve the RGB image data for each image, then use plt.imshow to do the rendering instead of sp.imshow.

bogatron
  • 18,639
  • 6
  • 53
  • 47
  • Thanks, this does iterate and plot all my samples thanks! Yet even changing the subplot(len(targets), 2, k +1) axes are on top of each other. Tried squeeze, sharex=True, and modifying fig size but did not work. – JuanCMontesH Jun 06 '22 at 06:48
  • I don't follow the comment about axes being on top of each other. Also, if you change the number of columns to 2 (as in your comment), then you'll probably wan to change the third arg to `plt.subplot` as well, since it is simply counting grid cells as a flat index (starting at 1) in row major order. – bogatron Jun 06 '22 at 15:14