Skip to content

Instantly share code, notes, and snippets.

@smidm
Created October 27, 2017 09:32
Show Gist options
  • Save smidm/745b4006a54cfe7343f4a50855547cc3 to your computer and use it in GitHub Desktop.
Save smidm/745b4006a54cfe7343f4a50855547cc3 to your computer and use it in GitHub Desktop.
shows figure in matplotlib and waits for keypress or mouse click to continue, on figure close exits the loop
import matplotlib.pylab as plt
import numpy as np
fig = plt.figure()
closed = False
def handle_close(evt):
global closed
closed = True
def waitforbuttonpress():
while plt.waitforbuttonpress(0.2) is None:
if closed:
return False
return True
fig.canvas.mpl_connect('close_event', handle_close)
while True:
plt.imshow(np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8))
plt.draw()
if not waitforbuttonpress():
break
print('.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment