0

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.

2
  • matplotlib.org/examples/pylab_examples/ginput_demo.html Commented Feb 28, 2016 at 2:31
  • Thanks for the comment. Actually I am using ginput for mouse coordinates but I need key listeners, too. I want to create key shortcuts for basic operations like increase/decrease brightness, save image, load next image, ... Commented Feb 28, 2016 at 2:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't this very similar to the code I have? Basically I want a key listener and after pressing 'n' the for loop (for img in images) should continue but so far I were not able to implement this. plot.show(block=True) blocks like forever - I could not release its block.
and in this case, close the window and it will continue in the loop
This looks like it could solve my problem. Of course closing the window continues the loop but then the user would have to do an additional (unnecessary) click.
You can bind n to fig.close()
fig.show() does not block the window for showing plot multiple times. so, Quick fixes using self.fig.waitforbuttonpress() it waits user to press the button for next plot visualisation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.