Skip to content

Commit 96d3605

Browse files
committed
ENH : add function to add displayhook for IPython
- add `draw_all` function to pyplot - add `install_ipython_repl_hook` to pyplot which when run monkey-patches the ipython DisplayHook to call draw_all when ever the repl comes back.
1 parent 389e697 commit 96d3605

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/matplotlib/pyplot.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import sys
2424
import warnings
25+
import types
2526

2627
import matplotlib
2728
import matplotlib.colorbar
@@ -108,6 +109,35 @@ def _backend_selection():
108109
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
109110

110111

112+
def install_ipython_repl_hook():
113+
try:
114+
from IPython.core.displayhook import DisplayHook
115+
except ImportError:
116+
return
117+
dh = sys.displayhook
118+
# make sure we really have an IPython thing
119+
if not isinstance(dh, DisplayHook):
120+
return
121+
122+
orig_func = type(dh).finish_displayhook
123+
124+
def finish_displayhook(self):
125+
draw_all()
126+
return orig_func(self)
127+
128+
dh.finish_displayhook = types.MethodType(finish_displayhook, dh)
129+
130+
131+
def draw_all():
132+
"""
133+
Redraw all figures registered with the pyplot
134+
state machine.
135+
"""
136+
for f_mgr in _pylab_helpers.Gcf.get_all_fig_managers():
137+
# TODO add logic to check if figure is dirty
138+
f_mgr.canvas.draw()
139+
140+
111141
@docstring.copy_dedent(Artist.findobj)
112142
def findobj(o=None, match=None, include_self=True):
113143
if o is None:

0 commit comments

Comments
 (0)