Skip to content

Commit e1edb58

Browse files
authored
Allow exiting with Ctrl+C when visualization is on (#131)
Effectively does the same thing as hitting Ctrl+C in the terminal, but doesn't require fighting for focus with the matplotlib window.
1 parent 7552cee commit e1edb58

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pyro/util/plot_tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import matplotlib.pyplot as plt
77
from mpl_toolkits.axes_grid1 import ImageGrid
88

9+
from pyro.util import msg
10+
11+
12+
# This is defined in the module scope instead of as a closure in setup_axes()
13+
# so it doesn't get added to the list of callbacks multiple times.
14+
def _key_handler(event):
15+
if event.key == "ctrl+c":
16+
msg.fail("ABORT: KeyboardInterrupt")
17+
918

1019
def setup_axes(myg, num):
1120
""" create a grid of axes whose layout depends on the aspect ratio of the
@@ -15,6 +24,7 @@ def setup_axes(myg, num):
1524
L_y = myg.ymax - myg.ymin
1625

1726
f = plt.figure(1)
27+
f.canvas.mpl_connect("key_press_event", _key_handler)
1828

1929
cbar_title = False
2030

0 commit comments

Comments
 (0)