8000 Merge pull request #15 from anntzer/udif · ipython/matplotlib-inline@f764b43 · GitHub
[go: up one dir, main page]

Skip to content

Commit f764b43

Browse files 8000
authored
Merge pull request #15 from anntzer/udif
Move draw_if_interactive logic to new_figure_manager_given_figure.
2 parents 170a075 + 6380cd9 commit f764b43

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

matplotlib_inline/backend_inline.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
# Distributed under the terms of the BSD 3-Clause License.
55

66
import matplotlib
7-
from matplotlib.backends.backend_agg import ( # noqa
8-
new_figure_manager,
9-
FigureCanvasAgg,
10-
new_figure_manager_given_figure,
11-
)
127
from matplotlib import colors
8+
from matplotlib.backends import backend_agg
9+
from matplotlib.backends.backend_agg import FigureCanvasAgg
1310
from matplotlib._pylab_helpers import Gcf
11+
from matplotlib.figure import Figure
1412

1513
from IPython.core.interactiveshell import InteractiveShell
1614
from IPython.core.getipython import get_ipython
@@ -20,6 +18,57 @@
2018
from .config import InlineBackend
2119

2220

21+
def new_figure_manager(num, *args, FigureClass=Figure, **kwargs):
22+
"""
23+
Return a new figure manager for a new figure instance.
24+
25+
This function is part of the API expected by Matplotlib backends.
26+
"""
27+
return new_figure_manager_given_figure(num, FigureClass(*args, **kwargs))
28+
29+
30+
def new_figure_manager_given_figure(num, figure):
31+
"""
32+
Return a new figure manager for a given figure instance.
33+
34+
This function is part of the API expected by Matplotlib backends.
35+
"""
36+
manager = backend_agg.new_figure_manager_given_figure(num, figure)
37+
38+
# Hack: matplotlib FigureManager objects in interacive backends (at least
39+
# in some of them) monkeypatch the figure object and add a .show() method
40+
# to it. This applies the same monkeypatch in order to support user code
41+
# that might expect `.show()` to be part of the official API of figure
42+
# objects. For further reference:
43+
# https://github.com/ipython/ipython/issues/1612
44+
# https://github.com/matplotlib/matplotlib/issues/835
45+
46+
if not hasattr(figure, 'show'):
47+
# Queue up `figure` for display
48+
figure.show = lambda *a: display(
49+
figure, metadata=_fetch_figure_metadata(figure))
50+
51+
# If matplotlib was manually set to non-interactive mode, this function
52+
# should be a no-op (otherwise we'll generate duplicate plots, since a user
53+
# who set ioff() manually expects to make separate draw/show calls).
54+
if not matplotlib.is_interactive():
55+
return
56+
57+
# ensure current figure will be drawn, and each subsequent call
58+
# of draw_if_interactive() moves the active figure to ensure it is
59+
# drawn last
60+
try:
61+
show._to_draw.remove(figure)
62+
except ValueError:
63+
# ensure it only appears in the draw list once
64+
pass
65+
# Queue up the figure for drawing in next show() call
66+
show._to_draw.append(figure)
67+
show._draw_called = True
68+
69+
return manager
70+
71+
2372
def show(close=None, block=None):
2473
"""Show all figures as SVG/PNG payloads sent to the IPython clients.
2574
@@ -56,51 +105,6 @@ def show(close=None, block=None):
56105
show._to_draw = []
57106

58107

59-
def draw_if_interactive():
60-
"""
61-
Is called after every pylab drawing command
62-
"""
63-
# signal that the current active figure should be sent at the end of
64-
# execution. Also sets the _draw_called flag, signaling that there will be
65-
# something to send. At the end of the code execution, a separate call to
66-
# flush_figures() will act upon these values
67-
manager = Gcf.get_active()
68-
if manager is None:
69-
return
70-
fig = manager.canvas.figure
71-
72-
# Hack: matplotlib FigureManager objects in interacive backends (at least
73-
# in some of them) monkeypatch the figure object and add a .show() method
74-
# to it. This applies the same monkeypatch in order to support user code
75-
# that might expect `.show()` to be part of the official API of figure
76-
# objects.
77-
# For further reference:
78-
# https://github.com/ipython/ipython/issues/1612
79-
# https://github.com/matplotlib/matplotlib/issues/835
80-
81-
if not hasattr(fig, 'show'):
82-
# Queue up `fig` for display
83-
fig.show = lambda *a: display(fig, metadata=_fetch_figure_metadata(fig))
84-
85-
# If matplotlib was manually set to non-interactive mode, this function
86-
# should be a no-op (otherwise we'll generate duplicate plots, since a user
87-
# who set ioff() manually expects to make separate draw/show calls).
88-
if not matplotlib.is_interactive():
89-
return
90-
91-
# ensure current figure will be drawn, and each subsequent call
92-
# of draw_if_interactive() moves the active figure to ensure it is
93-
# drawn last
94-
try:
95-
show._to_draw.remove(fig)
96-
except ValueError:
97-
# ensure it only appears in the draw list once
98-
pass
99-
# Queue up the figure for drawing in next show() call
100-
show._to_draw.append(fig)
101-
show._draw_called = True
102-
103-
104108
def flush_figures():
105109
"""Send all figures that changed
106110

0 commit comments

Comments
 (0)
0