@@ -108,9 +108,8 @@ def _backend_selection():
108
108
from matplotlib .backends import pylab_setup
109
109
_backend_mod , new_figure_manager , draw_if_interactive , _show = pylab_setup ()
110
110
111
- _BASE_DH = None
112
111
_IP_REGISTERED = None
113
-
112
+ _INSTALL_FIG_OBSERVER = False
114
113
115
114
def install_repl_displayhook ():
116
115
"""
@@ -119,8 +118,8 @@ def install_repl_displayhook():
119
118
120
119
This works with both IPython terminals and vanilla python shells.
121
120
"""
122
- global _BASE_DH
123
121
global _IP_REGISTERED
122
+ global _INSTALL_FIG_OBSERVER
124
123
125
124
class _NotIPython (Exception ):
126
125
pass
@@ -136,33 +135,23 @@ class _NotIPython(Exception):
136
135
if _IP_REGISTERED :
137
136
return
138
137
139
- def displayhook ():
138
+ def post_execute ():
140
139
if matplotlib .is_interactive ():
141
140
draw_all ()
142
141
143
142
# IPython >= 2
144
143
try :
145
- ip .events .register ('post_execute' , displayhook )
144
+ ip .events .register ('post_execute' , post_execute )
146
145
except AttributeError :
147
146
# IPython 1.x
148
- ip .register_post_execute (displayhook )
147
+ ip .register_post_execute (post_execute )
149
148
150
- _IP_REGISTERED = displayhook
149
+ _IP_REGISTERED = post_execute
150
+ _INSTALL_FIG_OBSERVER = False
151
151
152
152
# import failed or ipython is not running
153
153
except (ImportError , _NotIPython ):
154
-
155
- if _BASE_DH is not None :
156
- return
157
-
158
- dh = _BASE_DH = sys .displayhook
159
-
160
- def displayhook (* args ):
161
- dh (* args )
162
- if matplotlib .is_interactive ():
163
- draw_all ()
164
-
165
- sys .displayhook = displayhook
154
+ _INSTALL_FIG_OBSERVER = True
166
155
167
156
168
157
def uninstall_repl_displayhook ():
@@ -181,8 +170,8 @@ def uninstall_repl_displayhook():
181
170
function was there when matplotlib installed it's displayhook,
182
171
possibly discarding your changes.
183
172
"""
184
- global _BASE_DH
185
173
global _IP_REGISTERED
174
+ global _INSTALL_FIG_OBSERVER
186
175
if _IP_REGISTERED :
187
176
from IPython import get_ipython
188
177
ip = get_ipython ()
@@ -193,9 +182,8 @@ def uninstall_repl_displayhook():
193
182
"in IPython < 2.0" )
194
183
_IP_REGISTERED = None
195
184
196
- if _BASE_DH :
197
- sys .displayhook = _BASE_DH
198
- _BASE_DH = None
185
+ if _INSTALL_FIG_OBSERVER :
186
+ _INSTALL_FIG_OBSERVER = False
199
187
200
188
201
189
draw_all = _pylab_helpers .Gcf .draw_all
@@ -288,7 +276,8 @@ def pause(interval):
288
276
figManager = _pylab_helpers .Gcf .get_active ()
289
277
if figManager is not None :
290
278
canvas = figManager .canvas
291
- canvas .draw ()
279
+ if canvas .figure .stale :
280
+ canvas .draw ()
292
281
show (block = False )
293
282
canvas .start_event_loop (interval )
294
283
return
@@ -507,8 +496,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
507
496
if figManager is None :
508
497
max_open_warning = rcParams ['figure.max_open_warning' ]
509
498
510
- if (max_open_warning >= 1 and
511
- len (allnums ) >= max_open_warning ):
499
+ if (max_open_warning >= 1 and len (allnums ) >= max_open_warning ):
512
500
warnings .warn (
513
501
"More than %d figures have been opened. Figures "
514
502
"created through the pyplot interface "
@@ -543,9 +531,26 @@ def make_active(event):
543
531
_pylab_helpers .Gcf .set_active (figManager )
544
532
figManager .canvas .figure .number = num
545
533
534
+ if _INSTALL_FIG_OBSERVER :
535
+ figManager .canvas .figure .add_callback (_auto_draw_if_interactive )
536
+
546
537
return figManager .canvas .figure
547
538
548
539
540
+ def _auto_draw_if_interactive (fig ):
541
+ """
542
+ This is an internal helper function for making sure that auto-redrawing
543
+ works as intended in the plain python repl.
544
+
545
+ Parameters
546
+ ----------
547
+ fig : Figure
548
+ A figure object which is assumed to be associated with a canvas
549
+ """
550
+ if fig .stale and matplotlib .is_interactive ():
551
+ fig .canvas .draw_idle ()
552
+
553
+
549
554
def gcf ():
550
555
"Get a reference to the current figure."
551
556
0 commit comments