backend_inline.py
212 lines
| 8.0 KiB
| text/x-python
|
PythonLexer
|
r2890 | """Produce SVG versions of active plots for display by the rich Qt frontend. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r2987 | from __future__ import print_function | ||
|
r2890 | |||
|
r3731 | # Third-party imports | ||
|
r2987 | import matplotlib | ||
|
r8388 | from matplotlib.backends.backend_agg import new_figure_manager, FigureCanvasAgg | ||
|
r2756 | from matplotlib._pylab_helpers import Gcf | ||
# Local imports. | ||||
|
r3973 | from IPython.config.configurable import SingletonConfigurable | ||
|
r8157 | from IPython.core.display import display | ||
|
r3277 | from IPython.core.displaypub import publish_display_data | ||
|
r5469 | from IPython.core.pylabtools import print_figure, select_figure_format | ||
|
r10802 | from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum, Bool | ||
|
r5227 | from IPython.utils.warn import warn | ||
|
r3973 | #----------------------------------------------------------------------------- | ||
# Configurable for inline backend options | ||||
#----------------------------------------------------------------------------- | ||||
|
r5227 | # inherit from InlineBackendConfig for deprecation purposes | ||
|
r3973 | class InlineBackendConfig(SingletonConfigurable): | ||
|
r5227 | pass | ||
class InlineBackend(InlineBackendConfig): | ||||
|
r3973 | """An object to store configuration of the inline backend.""" | ||
|
r5227 | def _config_changed(self, name, old, new): | ||
# warn on change of renamed config section | ||||
if new.InlineBackendConfig != old.InlineBackendConfig: | ||||
warn("InlineBackendConfig has been renamed to InlineBackend") | ||||
super(InlineBackend, self)._config_changed(name, old, new) | ||||
|
r3973 | # The typical default figure size is too large for inline use, | ||
# so we shrink the figure size to 6x4, and tweak fonts to | ||||
|
r5292 | # make that fit. | ||
|
r3973 | rc = Dict({'figure.figsize': (6.0,4.0), | ||
|
r7798 | # play nicely with white background in the Qt and notebook frontend | ||
'figure.facecolor': 'white', | ||||
'figure.edgecolor': 'white', | ||||
|
r3973 | # 12pt labels get cutoff on 6x4 logplots, so use 10pt. | ||
'font.size': 10, | ||||
|
r5292 | # 72 dpi matches SVG/qtconsole | ||
# this only affects PNG export, as SVG has no dpi setting | ||||
'savefig.dpi': 72, | ||||
|
r3973 | # 10pt still needs a little more room on the xlabel: | ||
'figure.subplot.bottom' : .125 | ||||
}, config=True, | ||||
help="""Subset of matplotlib rcParams that should be different for the | ||||
inline backend.""" | ||||
) | ||||
|
r5292 | |||
|
r10802 | figure_format = CaselessStrEnum(['svg', 'png', 'retina'], default_value='png', config=True, | ||
|
r3973 | help="The image format for figures with the inline backend.") | ||
|
r4005 | def _figure_format_changed(self, name, old, new): | ||
|
r3973 | if self.shell is None: | ||
return | ||||
else: | ||||
select_figure_format(self.shell, new) | ||||
|
r5087 | |||
|
r10802 | close_figures = Bool(True, config=True, | ||
|
r5087 | help="""Close all figures at the end of each cell. | ||
When True, ensures that each cell starts with no active figures, but it | ||||
also means that one must keep track of references in order to edit or | ||||
redraw figures in subsequent cells. This mode is ideal for the notebook, | ||||
where residual plots from other cells might be surprising. | ||||
When False, one must call figure() to create new figures. This means | ||||
that gcf() and getfigs() can reference figures created in other cells, | ||||
and the active figure can continue to be edited with pylab/pyplot | ||||
methods that reference the current active figure. This mode facilitates | ||||
iterative editing of figures, and behaves most consistently with | ||||
other matplotlib backends, but figure barriers between cells must | ||||
be explicit. | ||||
""") | ||||
|
r3973 | |||
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | ||||
|
r2756 | |||
|
r2890 | #----------------------------------------------------------------------------- | ||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
|
r2756 | |||
|
r5087 | def show(close=None): | ||
"""Show all figures as SVG/PNG payloads sent to the IPython clients. | ||||
|
r2987 | |||
Parameters | ||||
---------- | ||||
close : bool, optional | ||||
If true, a ``plt.close('all')`` call is automatically issued after | ||||
|
r5086 | sending all the figures. If this is set, the figures will entirely | ||
|
r3280 | removed from the internal list of figures. | ||
|
r2756 | """ | ||
|
r5087 | if close is None: | ||
|
r5227 | close = InlineBackend.instance().close_figures | ||
|
r5346 | try: | ||
for figure_manager in Gcf.get_all_fig_managers(): | ||||
|
r8157 | display(figure_manager.canvas.figure) | ||
|
r5346 | finally: | ||
show._to_draw = [] | ||||
if close: | ||||
matplotlib.pyplot.close('all') | ||||
|
r5087 | |||
|
r2987 | |||
|
r3280 | |||
|
r2987 | # This flag will be reset by draw_if_interactive when called | ||
show._draw_called = False | ||||
|
r5086 | # list of figures to draw when flush_figures is called | ||
show._to_draw = [] | ||||
|
r2987 | |||
def draw_if_interactive(): | ||||
""" | ||||
Is called after every pylab drawing command | ||||
""" | ||||
|
r6517 | # 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 | ||||
|
r5086 | fig = Gcf.get_active().canvas.figure | ||
|
r6517 | |||
# 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 | ||||
|
r5086 | |||
|
r6517 | if not hasattr(fig, 'show'): | ||
# Queue up `fig` for display | ||||
|
r8157 | fig.show = lambda *a: display(fig) | ||
|
r6517 | |||
# 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 | ||||
|
r5086 | # 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 | ||||
|
r6517 | # Queue up the figure for drawing in next show() call | ||
|
r5086 | show._to_draw.append(fig) | ||
|
r2987 | show._draw_called = True | ||
|
r6517 | |||
|
r3973 | def flush_figures(): | ||
|
r5086 | """Send all figures that changed | ||
|
r2987 | |||
This is meant to be called automatically and will call show() if, during | ||||
prior code execution, there had been any calls to draw_if_interactive. | ||||
|
r5735 | |||
This function is meant to be used as a post_execute callback in IPython, | ||||
so user-caused errors are handled with showtraceback() instead of being | ||||
allowed to raise. If this function is not called from within IPython, | ||||
then these exceptions will raise. | ||||
|
r2987 | """ | ||
|
r5086 | if not show._draw_called: | ||
return | ||||
|
r5087 | |||
|
r5227 | if InlineBackend.instance().close_figures: | ||
|
r5087 | # ignore the tracking, just draw and close all figures | ||
|
r5735 | try: | ||
return show(True) | ||||
except Exception as e: | ||||
# safely show traceback if in IPython, else raise | ||||
try: | ||||
|
r5736 | get_ipython | ||
|
r5735 | except NameError: | ||
raise e | ||||
|
r5736 | else: | ||
get_ipython().showtraceback() | ||||
return | ||||
|
r5346 | try: | ||
# exclude any figures that were closed: | ||||
active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()]) | ||||
for fig in [ fig for fig in show._to_draw if fig in active ]: | ||||
|
r5735 | try: | ||
|
r8157 | display(fig) | ||
|
r5735 | except Exception as e: | ||
# safely show traceback if in IPython, else raise | ||||
try: | ||||
|
r5736 | get_ipython | ||
|
r5735 | except NameError: | ||
raise e | ||||
|
r5736 | else: | ||
get_ipython().showtraceback() | ||||
break | ||||
|
r5346 | finally: | ||
# clear flags for next round | ||||
show._to_draw = [] | ||||
show._draw_called = False | ||||
|
r3280 | |||
|
r8388 | |||
|
r8389 | # Changes to matplotlib in version 1.2 requires a mpl backend to supply a default | ||
# figurecanvas. This is set here to a Agg canvas | ||||
# See https://github.com/matplotlib/matplotlib/pull/1125 | ||||
|
r8388 | FigureCanvas = FigureCanvasAgg | ||