@@ -109,7 +109,7 @@ def _backend_selection():
109
109
_backend_mod , new_figure_manager , draw_if_interactive , _show = pylab_setup ()
110
110
111
111
_BASE_DH = None
112
- _IP_REGISTERED = False
112
+ _IP_REGISTERED = None
113
113
114
114
115
115
def install_repl_displayhook ():
@@ -136,14 +136,18 @@ class _NotIPython(Exception):
136
136
if _IP_REGISTERED :
137
137
return
138
138
139
+ def displayhook ():
140
+ if matplotlib .is_interactive ():
141
+ draw_all ()
142
+
139
143
# IPython >= 2
140
144
try :
141
- ip .events .register ('post_execute' , draw_all )
145
+ ip .events .register ('post_execute' , displayhook )
142
146
except AttributeError :
143
147
# IPython 1.x
144
- ip .register_post_execute (draw_all )
148
+ ip .register_post_execute (displayhook )
145
149
finally :
146
- _IP_REGISTERED = True
150
+ _IP_REGISTERED = displayhook
147
151
148
152
# import failed or ipython is not running
149
153
except (ImportError , _NotIPython ):
@@ -155,7 +159,8 @@ class _NotIPython(Exception):
155
159
156
160
def displayhook (* args ):
157
161
dh (* args )
158
- draw_all ()
162
+ if matplotlib .is_interactive ():
163
+ draw_all ()
159
164
160
165
sys .displayhook = displayhook
161
166
@@ -182,11 +187,11 @@ def uninstall_repl_displayhook():
182
187
from IPython import get_ipython
183
188
ip = get_ipython ()
184
189
try :
185
- ip .events .unregister ('post_execute' , draw_all )
190
+ ip .events .unregister ('post_execute' , _IP_REGISTERED )
186
191
except AttributeError :
187
192
raise NotImplementedError ("Can not unregister events "
188
193
"in IPython < 2.0" )
189
- _IP_REGISTERED = False
194
+ _IP_REGISTERED = None
190
195
191
196
if _BASE_DH :
192
197
sys .displayhook = _BASE_DH
@@ -252,11 +257,13 @@ def isinteractive():
252
257
def ioff ():
253
258
'Turn interactive mode off.'
254
259
matplotlib .interactive (False )
260
+ uninstall_repl_displayhook ()
255
261
256
262
257
263
def ion ():
258
264
'Turn interactive mode on.'
259
265
matplotlib .interactive (True )
266
+ install_repl_displayhook ()
260
267
261
268
262
269
def pause (interval ):
@@ -3900,4 +3907,9 @@ def spectral():
3900
3907
draw_if_interactive ()
3901
3908
3902
3909
_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.
3903
3915
install_repl_displayhook ()
0 commit comments