Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54da8cc

Browse files
committed
ENH : integrate repl hook with is_interactive
- add calls to {un}install in plt.i{on, off} - registered functions consult `mpl.is_interactive` before triggering draws
1 parent 17d9ada commit 54da8cc

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

lib/matplotlib/pyplot.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _backend_selection():
109109
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
110110

111111
_BASE_DH = None
112-
_IP_REGISTERED = False
112+
_IP_REGISTERED = None
113113

114114

115115
def install_repl_displayhook():
@@ -136,14 +136,18 @@ class _NotIPython(Exception):
136136
if _IP_REGISTERED:
137137
return
138138

139+
def displayhook():
140+
if matplotlib.is_interactive():
141+
draw_all()
142+
139143
# IPython >= 2
140144
try:
141-
ip.events.register('post_execute', draw_all)
145+
ip.events.register('post_execute', displayhook)
142146
except AttributeError:
143147
# IPython 1.x
144-
ip.register_post_execute(draw_all)
148+
ip.register_post_execute(displayhook)
145149
finally:
146-
_IP_REGISTERED = True
150+
_IP_REGISTERED = displayhook
147151

148152
# import failed or ipython is not running
149153
except (ImportError, _NotIPython):
@@ -155,7 +159,8 @@ class _NotIPython(Exception):
155159

156160
def displayhook(*args):
157161
dh(*args)
158-
draw_all()
162+
if matplotlib.is_interactive():
163+
draw_all()
159164

160165
sys.displayhook = displayhook
161166

@@ -182,11 +187,11 @@ def uninstall_repl_displayhook():
182187
from IPython import get_ipython
183188
ip = get_ipython()
184189
try:
185-
ip.events.unregister('post_execute', draw_all)
190+
ip.events.unregister('post_execute', _IP_REGISTERED)
186191
except AttributeError:
187192
raise NotImplementedError("Can not unregister events "
188193
"in IPython < 2.0")
189-
_IP_REGISTERED = False
194+
_IP_REGISTERED = None
190195

191196
if _BASE_DH:
192197
sys.displayhook = _BASE_DH
@@ -252,11 +257,13 @@ def isinteractive():
252257
def ioff():
253258
'Turn interactive mode off.'
254259
matplotlib.interactive(False)
260+
uninstall_repl_displayhook()
255261

256262

257263
def ion():
258264
'Turn interactive mode on.'
259265
matplotlib.interactive(True)
266+
install_repl_displayhook()
260267

261268

262269
def pause(interval):
@@ -3900,4 +3907,9 @@ def spectral():
39003907
draw_if_interactive()
39013908

39023909
_setup_pyplot_info_docstrings()
3910+
# just to be safe. Interactive mode can be turned on without
3911+
# calling `plt.ion()` so register it again here.
3912+
# This is safe because multiple calls to `install_repl_displayhook`
3913+
# are no-ops and the registered function respect `mpl.is_interactive()`
3914+
# to determine if they should trigger a draw.
39033915
install_repl_displayhook()

0 commit comments

Comments
 (0)