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