##// END OF EJS Templates
Removing old CodeMirror version.
Removing old CodeMirror version.

File last commit:

r4530:1de26e13
r4573:56475d69
Show More
backend_inline.py
116 lines | 4.1 KiB | text/x-python | PythonLexer
Fernando Perez
Fix svg rich backend to correctly show multiple figures.
r2890 """Produce SVG versions of active plots for display by the rich Qt frontend.
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 from __future__ import print_function
Fernando Perez
Fix svg rich backend to correctly show multiple figures.
r2890
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756 # Standard library imports
Fernando Perez
Do not generate output for empty figures in Qt console....
r3731 import sys
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756
Fernando Perez
Do not generate output for empty figures in Qt console....
r3731 # Third-party imports
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 import matplotlib
MinRK
add InlineBackendConfig...
r3973 from matplotlib.backends.backend_agg import new_figure_manager
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756 from matplotlib._pylab_helpers import Gcf
# Local imports.
MinRK
add InlineBackendConfig...
r3973 from IPython.config.configurable import SingletonConfigurable
Brian Granger
Mostly final version of display data....
r3277 from IPython.core.displaypub import publish_display_data
MinRK
add InlineBackendConfig...
r3973 from IPython.lib.pylabtools import print_figure, select_figure_format
from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum
#-----------------------------------------------------------------------------
# Configurable for inline backend options
#-----------------------------------------------------------------------------
class InlineBackendConfig(SingletonConfigurable):
"""An object to store configuration of the inline backend."""
# The typical default figure size is too large for inline use,
# so we shrink the figure size to 6x4, and tweak fonts to
# make that fit. This is configurable via Global.pylab_inline_rc,
# or rather it will be once the zmq kernel is hooked up to
# the config system.
rc = Dict({'figure.figsize': (6.0,4.0),
# 12pt labels get cutoff on 6x4 logplots, so use 10pt.
'font.size': 10,
# 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."""
)
figure_format = CaselessStrEnum(['svg', 'png'], default_value='png', config=True,
help="The image format for figures with the inline backend.")
MinRK
fix double-encoding of png data...
r4005 def _figure_format_changed(self, name, old, new):
MinRK
add InlineBackendConfig...
r3973 if self.shell is None:
return
else:
select_figure_format(self.shell, new)
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756
Fernando Perez
Fix svg rich backend to correctly show multiple figures.
r2890 #-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756
Fernando Perez
Close all mpl figures after flushing them to the console....
r3733 def show(close=True):
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 """Show all figures as SVG payloads sent to the IPython clients.
Parameters
----------
close : bool, optional
If true, a ``plt.close('all')`` call is automatically issued after
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280 sending all the SVG figures. If this is set, the figures will entirely
removed from the internal list of figures.
epatters
Initial checkin of (not yet working) matplotlib payload backend and associated machinery.
r2756 """
Fernando Perez
Fix svg rich backend to correctly show multiple figures.
r2890 for figure_manager in Gcf.get_all_fig_managers():
MinRK
add InlineBackendConfig...
r3973 send_figure(figure_manager.canvas.figure)
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 if close:
matplotlib.pyplot.close('all')
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 # This flag will be reset by draw_if_interactive when called
show._draw_called = False
def draw_if_interactive():
"""
Is called after every pylab drawing command
"""
# We simply flag we were called and otherwise do nothing. At the end of
# the code execution, a separate call to show_close() will act upon this.
show._draw_called = True
MinRK
add InlineBackendConfig...
r3973 def flush_figures():
"""Call show, close all open figures, sending all figure images.
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
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.
"""
if show._draw_called:
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280 show()
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 show._draw_called = False
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280
MinRK
add InlineBackendConfig...
r3973 def send_figure(fig):
"""Draw the current figure and send it as a PNG payload.
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280 """
Fernando Perez
Do not generate output for empty figures in Qt console....
r3731 # For an empty figure, don't even bother calling figure_to_svg, to avoid
# big blank spaces in the qt console
if not fig.axes:
return
MinRK
add InlineBackendConfig...
r3973 fmt = InlineBackendConfig.instance().figure_format
data = print_figure(fig, fmt)
mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' }
mime = mimetypes[fmt]
Fernando Perez
Do not generate output for empty figures in Qt console....
r3731 # flush text streams before sending figures, helps a little with output
# synchronization in the console (though it's a bandaid, not a real sln)
sys.stdout.flush(); sys.stderr.flush()
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280 publish_display_data(
MinRK
add InlineBackendConfig...
r3973 'IPython.zmq.pylab.backend_inline.send_figure',
{mime : data}
Brian Granger
Lots of work on the display system, focused on pylab stuff....
r3280 )