-
-
Notifications
You must be signed in to change notification settings - Fork 33
Move draw_if_interactive logic to new_figure_manager_given_figure. #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,11 @@ | |
# Distributed under the terms of the BSD 3-Clause License. | ||
|
||
import matplotlib | ||
from matplotlib.backends.backend_agg import ( # noqa | ||
new_figure_manager, | ||
FigureCanvasAgg, | ||
new_figure_manager_given_figure, | ||
) | ||
from matplotlib import colors | ||
from matplotlib.backends import backend_agg | ||
from matplotlib.backends.backend_agg import FigureCanvasAgg | ||
from matplotlib._pylab_helpers import Gcf | ||
from matplotlib.figure import Figure | ||
|
||
from IPython.core.interactiveshell import InteractiveShell | ||
from IPython.core.getipython import get_ipython | ||
|
@@ -20,6 +18,57 @@ | |
from .config import InlineBackend | ||
|
||
|
||
def new_figure_manager(num, *args, FigureClass=Figure, **kwargs): | ||
""" | ||
Return a new figure manager for a new figure instance. | ||
|
||
This function is part of the API expected by Matplotlib backends. | ||
""" | ||
return new_figure_manager_given_figure(num, FigureClass(*args, **kwargs)) | ||
|
||
|
||
def new_figure_manager_given_figure(num, figure): | ||
""" | ||
Return a new figure manager for a given figure instance. | ||
|
||
This function is part of the API expected by Matplotlib backends. | ||
""" | ||
manager = backend_agg.new_figure_manager_given_figure(num, figure) | ||
fperez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Hack: matplotlib FigureManager objects in interacive backends (at least | ||
# in some of them) monkeypatch the figure object and add a .show() method | ||
# to it. This applies the same monkeypatch in order to support user code | ||
# that might expect `.show()` to be part of the official API of figure | ||
# objects. For further reference: | ||
# https://github.com/ipython/ipython/issues/1612 | ||
# https://github.com/matplotlib/matplotlib/issues/835 | ||
|
||
if not hasattr(figure, 'show'): | ||
# Queue up `figure` for display | ||
figure.show = lambda *a: display( | ||
figure, metadata=_fetch_figure_metadata(figure)) | ||
|
||
# If matplotlib was manually set to non-interactive mode, this function | ||
# should be a no-op (otherwise we'll generate duplicate plots, since a user | ||
# who set ioff() manually expects to make separate draw/show calls). | ||
if not matplotlib.is_interactive(): | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
# ensure current figure will be drawn, and each subsequent call | ||
# of draw_if_interactive() moves the active figure to ensure it is | ||
# drawn last | ||
try: | ||
show._to_draw.remove(figure) | ||
except ValueError: | ||
# ensure it only appears in the draw list once | ||
pass | ||
# Queue up the figure for drawing in next show() call | ||
show._to_draw.append(figure) | ||
show._draw_called = True | ||
|
||
return manager | ||
|
||
|
||
def show(close=None, block=None): | ||
"""Show all figures as SVG/PNG payloads sent to the IPython clients. | ||
|
||
|
@@ -56,51 +105,6 @@ def show(close=None, block=None): | |
show._to_draw = [] | ||
|
||
|
||
def draw_if_interactive(): | ||
""" | ||
Is called after every pylab drawing command | ||
""" | ||
# signal that the current active figure should be sent at the end of | ||
# execution. Also sets the _draw_called flag, signaling that there will be | ||
# something to send. At the end of the code execution, a separate call to | ||
# flush_figures() will act upon these values | ||
manager = Gcf.get_active() | ||
if manager is None: | ||
return | ||
fig = manager.canvas.figure | ||
|
||
# Hack: matplotlib FigureManager objects in interacive backends (at least | ||
# in some of them) monkeypatch the figure object and add a .show() method | ||
# to it. This applies the same monkeypatch in order to support user code | ||
# that might expect `.show()` to be part of the official API of figure | ||
# objects. | ||
# For further reference: | ||
# https://github.com/ipython/ipython/issues/1612 | ||
# https://github.com/matplotlib/matplotlib/issues/835 | ||
|
||
if not hasattr(fig, 'show'): | ||
# Queue up `fig` for display | ||
fig.show = lambda *a: display(fig, metadata=_fetch_figure_metadata(fig)) | ||
|
||
# If matplotlib was manually set to non-interactive mode, this function | ||
# should be a no-op (otherwise we'll generate duplicate plots, since a user | ||
# who set ioff() manually expects to make separate draw/show calls). | ||
if not matplotlib.is_interactive(): | ||
return | ||
|
||
# ensure current figure will be drawn, and each subsequent call | ||
# of draw_if_interactive() moves the active figure to ensure it is | ||
# drawn last | ||
try: | ||
show._to_draw.remove(fig) | ||
except ValueError: | ||
# ensure it only appears in the draw list once | ||
pass | ||
# Queue up the figure for drawing in next show() call | ||
show._to_draw.append(fig) | ||
show._draw_called = True | ||
|
||
|
||
def flush_figures(): | ||
"""Send all figures that changed | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.