@@ -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
115115def 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():
252257def ioff ():
253258 'Turn interactive mode off.'
254259 matplotlib .interactive (False )
260+ uninstall_repl_displayhook ()
255261
256262
257263def ion ():
258264 'Turn interactive mode on.'
259265 matplotlib .interactive (True )
266+ install_repl_displayhook ()
260267
261268
262269def 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.
39033915install_repl_displayhook ()
0 commit comments