spectralpython / spectral

Python module for hyperspectral image processing
MIT License
573 stars 139 forks source link

Importing spectral breaks matplotlib's plotting abilities #44

Closed MattFerraro closed 8 years ago

MattFerraro commented 8 years ago

This functions as expected, popping up a graphical plot:

import numpy as np
import matplotlib.pyplot as plt

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
plt.imshow(A, interpolation='nearest')
plt.show()

But this suppresses the graphical plot--it never shows up at all.

import spectral as spc      # <-- Only change is the addition of this line
import numpy as np
import matplotlib.pyplot as plt

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
plt.imshow(A, interpolation='nearest')
plt.show()

The suppressing behavior is consistent regardless of the import order.

Expected behavior is that importing spectral should have no bearing on the proper functioning of matplotlib's plots.

Working environment is on a mac, El Capitan, invoking from command line as python example.py from iterm2.

tboggs commented 8 years ago

I was able to reproduce this behavior using the standard python interpreter (2.7.6). It did not occur for me with ipython. This appears to be due to SPy turning on matplotlib's interactive mode in spectral.spectral._init.

I agree that simply importing the spectral module shouldn't affect matplotlib's default behavior. However, I think it is reasonable to expect that once a plotting command is issued via the spectral module, matplotlib's behavior may be affected (i.e., interactive mode will be enabled). So I will move the code that changes matplotlib to interactive mode such that it will only be called when a SPy plotting command is issued.

MattFerraro commented 8 years ago

Thanks @tboggs! This also suggests a solution which works for me in the meanwhile:

import spectral.io.envi as envi
from matplotlib import interactive
interactive(False)
import numpy as np
import matplotlib.pyplot as plt

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
plt.imshow(A, interpolation='nearest')
plt.show()
tboggs commented 8 years ago

Yes! I meant to suggest that as a work-around. I'm glad you thought of it too. I will have the fix in shortly. Just need to test it to make sure nothing else breaks...