I have a basic image viewer program in Python using matplotlib and the QT4Agg Backend. One of the functionalities is to generate the 2D-Power Spectrum from the 2D-FFT and place that in a second plot within one figure. What I would like to add is a button that takes the FFT image and pops it out into another window so it can be analyzed in greater detail.
My code currently is doing everything I need it to but it is also raising a QT exception or warning of some sort to the terminal (this is being run under OS X):
"QCoreApplication::exec: The event loop is already running"
def popout(event):
nfig, nfftax = pylab.subplots(1, 1)
imgtoconv = getArr(files[imgindex])
fftimg = np.abs(fftpack.fftshift(fftpack.fft2(imgtoconv)))**2
nfftax.imshow(np.log10(fftimg), cmap=cm.Greys_r)
pylab.show()
I have a button defined and linked to the popout() function via Button.on_clicked(popout) In the main loop of the program a figure is initiated and shown with two plots. So pylab.show() has been called once already
The problem I am having seems to be that I am calling pylab.show() again which is generating an exception or warning that the event loop is already initiated.
I tried using pylab.draw() instead of pylab.show() but when I do that the button click doesn't show the new figure
Is there an easy way to suppress this warning? Or is there another way to spawn the new figure and make it visible without recalling pylab.show()?
EDIT: I also tried adding nfig.canvas.draw() after I create the new figure and new axes then calling pylab.draw() at the end of the popout() function. This still is not showing the new figure onclick
EDIT2: Switching to use the TKAgg Backend and recalling pylab.show() makes the problem go away. Any ideas as to why this is only an issue in the QT4Agg Backend?
Have you tried using ion()? I had the same issue when using multiple figures from a button callback. For me, using this code inside the popout() function solved the problem:
import matplotlib.pyplot as p
import numpy as np
p.ion()
p.close(1)
p.close(2)
p.figure(1)
p.plot(np.random.normal(size=100))
p.figure(2)
p.plot(np.random.normal(size=100))
As I understand it, this forces matplotlib to use its own threads to handle all the figures, without conflicting with the main loop of your GUI. I close the figures to completely reset them and free the memory, but p.clf() works as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With