1
"""Produce SVG versions of active plots for display by the rich Qt frontend.
3
#-----------------------------------------------------------------------------
5
#-----------------------------------------------------------------------------
6
from __future__ import print_function
8
# Standard library imports
13
from matplotlib.backends.backend_agg import new_figure_manager
14
from matplotlib._pylab_helpers import Gcf
17
from IPython.config.configurable import SingletonConfigurable
18
from IPython.core.displaypub import publish_display_data
19
from IPython.lib.pylabtools import print_figure, select_figure_format
20
from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum
21
#-----------------------------------------------------------------------------
22
# Configurable for inline backend options
23
#-----------------------------------------------------------------------------
25
class InlineBackendConfig(SingletonConfigurable):
26
"""An object to store configuration of the inline backend."""
28
# The typical default figure size is too large for inline use,
29
# so we shrink the figure size to 6x4, and tweak fonts to
30
# make that fit. This is configurable via Global.pylab_inline_rc,
31
# or rather it will be once the zmq kernel is hooked up to
33
rc = Dict({'figure.figsize': (6.0,4.0),
34
# 12pt labels get cutoff on 6x4 logplots, so use 10pt.
36
# 10pt still needs a little more room on the xlabel:
37
'figure.subplot.bottom' : .125
39
help="""Subset of matplotlib rcParams that should be different for the
42
figure_format = CaselessStrEnum(['svg', 'png'], default_value='png', config=True,
43
help="The image format for figures with the inline backend.")
45
def _figure_format_changed(self, name, old, new):
46
if self.shell is None:
49
select_figure_format(self.shell, new)
51
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
54
#-----------------------------------------------------------------------------
56
#-----------------------------------------------------------------------------
59
"""Show all figures as SVG payloads sent to the IPython clients.
63
close : bool, optional
64
If true, a ``plt.close('all')`` call is automatically issued after
65
sending all the SVG figures. If this is set, the figures will entirely
66
removed from the internal list of figures.
68
for figure_manager in Gcf.get_all_fig_managers():
69
send_figure(figure_manager.canvas.figure)
71
matplotlib.pyplot.close('all')
74
# This flag will be reset by draw_if_interactive when called
75
show._draw_called = False
78
def draw_if_interactive():
80
Is called after every pylab drawing command
82
# We simply flag we were called and otherwise do nothing. At the end of
83
# the code execution, a separate call to show_close() will act upon this.
84
show._draw_called = True
88
"""Call show, close all open figures, sending all figure images.
90
This is meant to be called automatically and will call show() if, during
91
prior code execution, there had been any calls to draw_if_interactive.
95
show._draw_called = False
99
"""Draw the current figure and send it as a PNG payload.
101
# For an empty figure, don't even bother calling figure_to_svg, to avoid
102
# big blank spaces in the qt console
105
fmt = InlineBackendConfig.instance().figure_format
106
data = print_figure(fig, fmt)
107
mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' }
108
mime = mimetypes[fmt]
109
# flush text streams before sending figures, helps a little with output
110
# synchronization in the console (though it's a bandaid, not a real sln)
111
sys.stdout.flush(); sys.stderr.flush()
112
publish_display_data(
113
'IPython.zmq.pylab.backend_inline.send_figure',