~ubuntu-branches/debian/experimental/ipython/experimental

« back to all changes in this revision

Viewing changes to IPython/zmq/pylab/backend_inline.py

  • Committer: Bazaar Package Importer
  • Author(s): Julian Taylor
  • Date: 2011-05-10 16:18:45 UTC
  • mfrom: (1.2.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20110510161845-0nrh795uiyp1a20r
Tags: 0.11~rc1-1
* New upstream release candidate
* change to source format 3.0 (quilt)
* run testsuite during build
* add clean target
* build-depends:
  - matplotlib, pymongo, xauth, xvfb and qt4 for tests
  - graphviz, sphinx for docs
* add dependencies and don't install embedded copy:
  - python-argparse(Closes: #555348)
  - python-configobj (Closes: #555339)
  - python-simplegeneric
  - python-decorator
  - python-pyparsing
  - pexpect
* update patches
  - disabled obsolete patches 03, 04 and 06
  - refresh default background color patch 07
  - add patch to fix testsuite
  - add patch to improve error message on missing ipython-qtconsole
* change to compat 7
* split out -parallel package
* split out -qtconsole package
* split out -doc package
* improve dependencies for each package
* add doc-base control file
* fix the ipythonX.Y startup scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Produce SVG versions of active plots for display by the rich Qt frontend.
 
2
"""
 
3
#-----------------------------------------------------------------------------
 
4
# Imports
 
5
#-----------------------------------------------------------------------------
 
6
from __future__ import print_function
 
7
 
 
8
# Standard library imports
 
9
import sys
 
10
 
 
11
# Third-party imports
 
12
import matplotlib
 
13
from matplotlib.backends.backend_agg import new_figure_manager
 
14
from matplotlib._pylab_helpers import Gcf
 
15
 
 
16
# Local imports.
 
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
#-----------------------------------------------------------------------------
 
24
 
 
25
class InlineBackendConfig(SingletonConfigurable):
 
26
    """An object to store configuration of the inline backend."""
 
27
 
 
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
 
32
    # the config system.
 
33
    rc = Dict({'figure.figsize': (6.0,4.0),
 
34
        # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
 
35
        'font.size': 10,
 
36
        # 10pt still needs a little more room on the xlabel:
 
37
        'figure.subplot.bottom' : .125
 
38
        }, config=True,
 
39
        help="""Subset of matplotlib rcParams that should be different for the
 
40
        inline backend."""
 
41
    )
 
42
    figure_format = CaselessStrEnum(['svg', 'png'], default_value='png', config=True,
 
43
        help="The image format for figures with the inline backend.")
 
44
 
 
45
    def _figure_format_changed(self, name, old, new):
 
46
        if self.shell is None:
 
47
            return
 
48
        else:
 
49
            select_figure_format(self.shell, new)
 
50
 
 
51
    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
 
52
 
 
53
 
 
54
#-----------------------------------------------------------------------------
 
55
# Functions
 
56
#-----------------------------------------------------------------------------
 
57
 
 
58
def show(close=True):
 
59
    """Show all figures as SVG payloads sent to the IPython clients.
 
60
 
 
61
    Parameters
 
62
    ----------
 
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.
 
67
    """
 
68
    for figure_manager in Gcf.get_all_fig_managers():
 
69
        send_figure(figure_manager.canvas.figure)
 
70
    if close:
 
71
        matplotlib.pyplot.close('all')
 
72
 
 
73
 
 
74
# This flag will be reset by draw_if_interactive when called
 
75
show._draw_called = False
 
76
 
 
77
 
 
78
def draw_if_interactive():
 
79
    """
 
80
    Is called after every pylab drawing command
 
81
    """
 
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
 
85
 
 
86
 
 
87
def flush_figures():
 
88
    """Call show, close all open figures, sending all figure images.
 
89
 
 
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.
 
92
    """
 
93
    if show._draw_called:
 
94
        show()
 
95
        show._draw_called = False
 
96
 
 
97
 
 
98
def send_figure(fig):
 
99
    """Draw the current figure and send it as a PNG payload.
 
100
    """
 
101
    # For an empty figure, don't even bother calling figure_to_svg, to avoid
 
102
    # big blank spaces in the qt console
 
103
    if not fig.axes:
 
104
        return
 
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',
 
114
        'Matplotlib Plot',
 
115
        {mime : data}
 
116
    )
 
117