Skip to content

Commit 494e4c7

Browse files
committed
ENH: redraw callback on Figure in base repl
If we do not have IPython, then the only hook I know of in the repl is the display hook which is called when ever something is going to be displayed, not after the execution of the last command has finished (the reason it exists is to give users a chance to intercept and change the display of the output, not as a generic call back as we were trying to use it). In this case register a callback on the `Figure` artist at creation time to call `draw_idle` on the canvas if the Figure is stale. We are guaranteed to see this at least once when stale is flipped to True, but may see it more than once if other paths call `pchanged()`. Fortunately, `pchanged` is not widely used in the internal code base and even if `draw_idle` is called multiple times, we should be able to count on the backends handling multiple draw_idle commands correctly. Closes matplotlib#4504
1 parent 1b83af3 commit 494e4c7

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

‎lib/matplotlib/pyplot.py‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ def _backend_selection():
108108
from matplotlib.backends import pylab_setup
109109
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
110110

111-
_BASE_DH = None
112111
_IP_REGISTERED = None
113-
112+
_INSTALL_FIG_OBSERVER = False
114113

115114
def install_repl_displayhook():
116115
"""
@@ -119,8 +118,8 @@ def install_repl_displayhook():
119118
120119
This works with both IPython terminals and vanilla python shells.
121120
"""
122-
global _BASE_DH
123121
global _IP_REGISTERED
122+
global _INSTALL_FIG_OBSERVER
124123

125124
class _NotIPython(Exception):
126125
pass
@@ -148,21 +147,11 @@ def post_execute():
148147
ip.register_post_execute(post_execute)
149148

150149
_IP_REGISTERED = post_execute
150+
_INSTALL_FIG_OBSERVER = False
151151

152152
# import failed or ipython is not running
153153
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
166155

167156

168157
def uninstall_repl_displayhook():
@@ -181,8 +170,8 @@ def uninstall_repl_displayhook():
181170
function was there when matplotlib installed it's displayhook,
182171
possibly discarding your changes.
183172
"""
184-
global _BASE_DH
185173
global _IP_REGISTERED
174+
global _INSTALL_FIG_OBSERVER
186175
if _IP_REGISTERED:
187176
from IPython import get_ipython
188177
ip = get_ipython()
@@ -193,9 +182,8 @@ def uninstall_repl_displayhook():
193182
"in IPython < 2.0")
194183
_IP_REGISTERED = None
195184

196-
if _BASE_DH:
197-
sys.displayhook = _BASE_DH
198-
_BASE_DH = None
185+
if _INSTALL_FIG_OBSERVER:
186+
_INSTALL_FIG_OBSERVER = False
199187

200188

201189
draw_all = _pylab_helpers.Gcf.draw_all
@@ -541,6 +529,11 @@ def make_active(event):
541529

542530
_pylab_helpers.Gcf.set_active(figManager)
543531
figManager.canvas.figure.number = num
532+
if _INSTALL_FIG_OBSERVER:
533+
def auto_draw(fig):
534+
if fig.stale and matplotlib.is_interactive():
535+
fig.canvas.draw_idle()
536+
figManager.canvas.figure.add_callback(auto_draw)
544537

545538
return figManager.canvas.figure
546539

0 commit comments

Comments
 (0)