Reputation: 11002
I have a script which plots some image:
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
(...)
fig = plt.figure()
ax = fig.add_subplot(111)
(...)
for img in images:
imgdata = mpimg.imread(img)
ax.imshow(imgdata)
plt.show(block=True)
Unfortunately, it seems to be unpossible to block the GUI and wait for user input with fig.show()
. Just plt.show()
blocks...so I have to use the plt.show
solution?
Additionally I am listening to user events like this:
cid = fig.canvas.mpl_connect('key_press_event', on_key_press)
def on_key_press(event):
if event.key == "n":
ax.cla()
plt.show(block=False)
My intention is to listen for 'n' (like 'next') and then continue with the for loop, hence loading the next image data and plotting it, waiting for the user input again and so on...
However, the second call show()
(plt.show(block=False)
) updates the image (so the data of ax
is cleared) but the lock is not released - the for loop does not continue. How could I achieve this behavior?
EDIT: I need the event.xdata
, event.ydata
, event.x
, event.y
values of matplotlib
, so implementing a "hack" with something like input()
and therefore letting python wait for user input in the console (and using matplotlib with block=False
every time) would not solve my problem.
Upvotes: 0
Views: 2578
Reputation: 87396
I am not quite sure what behavior you want, but something like
def set_up_figure():
fig, ax = plt.subplots()
fig.canvas.mpl_connect(..)
fig.canvas.mpl_connect(..)
return fig, ax
for img in images:
fig, ax = set_up_figure()
ax.imshow(img)
plt.show(block=True)
might work.
What plt.show
does (that fig.show
does not) is start the GUI event loop which is what, in the end, takes the user input, shuffles it through its callback mechanism, hands the events to the mpl callback mechanism, which in turn actually runs your functions.
Upvotes: 1