spectralpython/spectral

Spectral plots in Jupyter Notebook

pythonic2020 opened this issue · 5 comments

Hello, I'm having trouble getting a spectral plot to display in a Jupyter Notebook. I'm running SPy 0.22.1 in an up-to-date Miniconda environment with notebook 6.1.5 and Python 3.8.6. I have not changed my matplotlib backend, and don't know if I can enable the required WX backend. My backend is probably causing my problem - I'm searching on how to enable the WX backend now. My data is in ENVI format with band wavelengths in the header file. This code:

%matplotlib notebook
import spectral.io.envi as envi
image = envi.open('hyper.hdr', image='hyper')
from spectral.graphics.spypylab import imshow 
view = imshow(data=image[1300:1800, 1700:2200], bands=(71, 84, 97),
 origin='upper', stretch=(0.02, 0.98), source=image, 
 title='New data - Bands 71, 84, 97/RGB')

displays data perfectly, and I can pan and zoom using matplotlib buttons. However, when I double-click on a pixel, nothing happens. I think a spectral plot should appear, but it does not.

If I enter this code in a new notebook cell:

zoom = view.open_zoom()

and ctrl+lclick in the original window, the zoom window updates as it should, so interactive communication between cells appears to be working.

Any ideas on how to get a spectral plot to display would be greatly appreciated.

Thanks in advance...

Never mind. After installing wxpython and pyopengl, and using this code:

import matplotlib as mpl
mpl.use('WXAgg')
import matplotlib.pyplot as plt
#%matplotlib notebook

I got the spectral plots (and view_cube) to work. Be sure to disable the matplotlib magic. Beautiful! :-)

I had no idea interactive features could work in a jupyter notebook. Glad you figured it out.

Hopefully my posts will help people get SPy going in notebooks in the future. The %matplotlib widget magic works very nicely for zooming and panning in notebooks, and for quickly saving plots to file!

SPy has suddenly become very important for my work, and I am just learning to use it on hyperspectral image data. However, I am having trouble with some very simple yet vital operations that aren't fully explained with examples in the documentation. Would it be possible for you to answer a few quick questions, perhaps via email or some other communications method? Posting questions here probably isn't appropriate. Your help would be greatly appreciated!

If they're questions that could be useful to others, then it's fine to ask them in issues on GitHub.

Excellent, will do. Thank you in advance. I'm amazed at the capability of SPy, especially the documentation of referenced articles for algorithms.