Show More
@@ -1,117 +1,118 | |||
|
1 | 1 | """Configurable for configuring the IPython inline backend |
|
2 | 2 | |
|
3 | 3 | This module does not import anything from matplotlib. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (C) 2011 The IPython Development Team |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | from IPython.config.configurable import SingletonConfigurable |
|
17 | 17 | from IPython.utils.traitlets import ( |
|
18 | 18 | Dict, Instance, CaselessStrEnum, Set, Bool, Int, TraitError, Unicode |
|
19 | 19 | ) |
|
20 | 20 | from IPython.utils.warn import warn |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Configurable for inline backend options |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | def pil_available(): |
|
27 | 27 | """Test if PIL/Pillow is available""" |
|
28 | 28 | out = False |
|
29 | 29 | try: |
|
30 | 30 | from PIL import Image |
|
31 | 31 | out = True |
|
32 | 32 | except: |
|
33 | 33 | pass |
|
34 | 34 | return out |
|
35 | 35 | |
|
36 | 36 | # inherit from InlineBackendConfig for deprecation purposes |
|
37 | 37 | class InlineBackendConfig(SingletonConfigurable): |
|
38 | 38 | pass |
|
39 | 39 | |
|
40 | 40 | class InlineBackend(InlineBackendConfig): |
|
41 | 41 | """An object to store configuration of the inline backend.""" |
|
42 | 42 | |
|
43 | 43 | def _config_changed(self, name, old, new): |
|
44 | 44 | # warn on change of renamed config section |
|
45 | 45 | if new.InlineBackendConfig != old.InlineBackendConfig: |
|
46 | 46 | warn("InlineBackendConfig has been renamed to InlineBackend") |
|
47 | 47 | super(InlineBackend, self)._config_changed(name, old, new) |
|
48 | 48 | |
|
49 | 49 | # The typical default figure size is too large for inline use, |
|
50 | 50 | # so we shrink the figure size to 6x4, and tweak fonts to |
|
51 | 51 | # make that fit. |
|
52 | 52 | rc = Dict({'figure.figsize': (6.0,4.0), |
|
53 | 53 | # play nicely with white background in the Qt and notebook frontend |
|
54 | 54 | 'figure.facecolor': (1,1,1,0), |
|
55 | 55 | 'figure.edgecolor': (1,1,1,0), |
|
56 | 'axes.facecolor': (1,1,1,0), | |
|
56 | 57 | # 12pt labels get cutoff on 6x4 logplots, so use 10pt. |
|
57 | 58 | 'font.size': 10, |
|
58 | 59 | # 72 dpi matches SVG/qtconsole |
|
59 | 60 | # this only affects PNG export, as SVG has no dpi setting |
|
60 | 61 | 'savefig.dpi': 72, |
|
61 | 62 | # 10pt still needs a little more room on the xlabel: |
|
62 | 63 | 'figure.subplot.bottom' : .125 |
|
63 | 64 | }, config=True, |
|
64 | 65 | help="""Subset of matplotlib rcParams that should be different for the |
|
65 | 66 | inline backend.""" |
|
66 | 67 | ) |
|
67 | 68 | |
|
68 | 69 | figure_formats = Set({'png'}, config=True, |
|
69 | 70 | help="""A set of figure formats to enable: 'png', |
|
70 | 71 | 'retina', 'jpeg', 'svg', 'pdf'.""") |
|
71 | 72 | |
|
72 | 73 | def _update_figure_formatters(self): |
|
73 | 74 | if self.shell is not None: |
|
74 | 75 | from IPython.core.pylabtools import select_figure_formats |
|
75 | 76 | select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs) |
|
76 | 77 | |
|
77 | 78 | def _figure_formats_changed(self, name, old, new): |
|
78 | 79 | if 'jpg' in new or 'jpeg' in new: |
|
79 | 80 | if not pil_available(): |
|
80 | 81 | raise TraitError("Requires PIL/Pillow for JPG figures") |
|
81 | 82 | self._update_figure_formatters() |
|
82 | 83 | |
|
83 | 84 | figure_format = Unicode(config=True, help="""The figure format to enable (deprecated |
|
84 | 85 | use `figure_formats` instead)""") |
|
85 | 86 | |
|
86 | 87 | def _figure_format_changed(self, name, old, new): |
|
87 | 88 | if new: |
|
88 | 89 | self.figure_formats = {new} |
|
89 | 90 | |
|
90 | 91 | print_figure_kwargs = Dict({'bbox_inches' : 'tight'}, config=True, |
|
91 | 92 | help="""Extra kwargs to be passed to fig.canvas.print_figure. |
|
92 | 93 | |
|
93 | 94 | Logical examples include: bbox_inches, quality (for jpeg figures), etc. |
|
94 | 95 | """ |
|
95 | 96 | ) |
|
96 | 97 | _print_figure_kwargs_changed = _update_figure_formatters |
|
97 | 98 | |
|
98 | 99 | close_figures = Bool(True, config=True, |
|
99 | 100 | help="""Close all figures at the end of each cell. |
|
100 | 101 | |
|
101 | 102 | When True, ensures that each cell starts with no active figures, but it |
|
102 | 103 | also means that one must keep track of references in order to edit or |
|
103 | 104 | redraw figures in subsequent cells. This mode is ideal for the notebook, |
|
104 | 105 | where residual plots from other cells might be surprising. |
|
105 | 106 | |
|
106 | 107 | When False, one must call figure() to create new figures. This means |
|
107 | 108 | that gcf() and getfigs() can reference figures created in other cells, |
|
108 | 109 | and the active figure can continue to be edited with pylab/pyplot |
|
109 | 110 | methods that reference the current active figure. This mode facilitates |
|
110 | 111 | iterative editing of figures, and behaves most consistently with |
|
111 | 112 | other matplotlib backends, but figure barriers between cells must |
|
112 | 113 | be explicit. |
|
113 | 114 | """) |
|
114 | 115 | |
|
115 | 116 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
116 | 117 | |
|
117 | 118 |
General Comments 0
You need to be logged in to leave comments.
Login now