##// END OF EJS Templates
fixup
MinRK -
Show More
@@ -1,373 +1,372 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Pylab (matplotlib) support utilities.
2 """Pylab (matplotlib) support utilities.
3
3
4 Authors
4 Authors
5 -------
5 -------
6
6
7 * Fernando Perez.
7 * Fernando Perez.
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10 from __future__ import print_function
10 from __future__ import print_function
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2009 The IPython Development Team
13 # Copyright (C) 2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import sys
23 import sys
24 from io import BytesIO
24 from io import BytesIO
25
25
26 from IPython.core.display import _pngxy
26 from IPython.core.display import _pngxy
27 from IPython.utils.decorators import flag_calls
27 from IPython.utils.decorators import flag_calls
28 from IPython.utils import py3compat
28 from IPython.utils import py3compat
29
29
30 # If user specifies a GUI, that dictates the backend, otherwise we read the
30 # If user specifies a GUI, that dictates the backend, otherwise we read the
31 # user's mpl default from the mpl rc structure
31 # user's mpl default from the mpl rc structure
32 backends = {'tk': 'TkAgg',
32 backends = {'tk': 'TkAgg',
33 'gtk': 'GTKAgg',
33 'gtk': 'GTKAgg',
34 'gtk3': 'GTK3Agg',
34 'gtk3': 'GTK3Agg',
35 'wx': 'WXAgg',
35 'wx': 'WXAgg',
36 'qt': 'Qt4Agg', # qt3 not supported
36 'qt': 'Qt4Agg', # qt3 not supported
37 'qt4': 'Qt4Agg',
37 'qt4': 'Qt4Agg',
38 'osx': 'MacOSX',
38 'osx': 'MacOSX',
39 'inline' : 'module://IPython.kernel.zmq.pylab.backend_inline'}
39 'inline' : 'module://IPython.kernel.zmq.pylab.backend_inline'}
40
40
41 # We also need a reverse backends2guis mapping that will properly choose which
41 # We also need a reverse backends2guis mapping that will properly choose which
42 # GUI support to activate based on the desired matplotlib backend. For the
42 # GUI support to activate based on the desired matplotlib backend. For the
43 # most part it's just a reverse of the above dict, but we also need to add a
43 # most part it's just a reverse of the above dict, but we also need to add a
44 # few others that map to the same GUI manually:
44 # few others that map to the same GUI manually:
45 backend2gui = dict(zip(backends.values(), backends.keys()))
45 backend2gui = dict(zip(backends.values(), backends.keys()))
46 # Our tests expect backend2gui to just return 'qt'
46 # Our tests expect backend2gui to just return 'qt'
47 backend2gui['Qt4Agg'] = 'qt'
47 backend2gui['Qt4Agg'] = 'qt'
48 # In the reverse mapping, there are a few extra valid matplotlib backends that
48 # In the reverse mapping, there are a few extra valid matplotlib backends that
49 # map to the same GUI support
49 # map to the same GUI support
50 backend2gui['GTK'] = backend2gui['GTKCairo'] = 'gtk'
50 backend2gui['GTK'] = backend2gui['GTKCairo'] = 'gtk'
51 backend2gui['GTK3Cairo'] = 'gtk3'
51 backend2gui['GTK3Cairo'] = 'gtk3'
52 backend2gui['WX'] = 'wx'
52 backend2gui['WX'] = 'wx'
53 backend2gui['CocoaAgg'] = 'osx'
53 backend2gui['CocoaAgg'] = 'osx'
54
54
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 # Matplotlib utilities
56 # Matplotlib utilities
57 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
58
58
59
59
60 def getfigs(*fig_nums):
60 def getfigs(*fig_nums):
61 """Get a list of matplotlib figures by figure numbers.
61 """Get a list of matplotlib figures by figure numbers.
62
62
63 If no arguments are given, all available figures are returned. If the
63 If no arguments are given, all available figures are returned. If the
64 argument list contains references to invalid figures, a warning is printed
64 argument list contains references to invalid figures, a warning is printed
65 but the function continues pasting further figures.
65 but the function continues pasting further figures.
66
66
67 Parameters
67 Parameters
68 ----------
68 ----------
69 figs : tuple
69 figs : tuple
70 A tuple of ints giving the figure numbers of the figures to return.
70 A tuple of ints giving the figure numbers of the figures to return.
71 """
71 """
72 from matplotlib._pylab_helpers import Gcf
72 from matplotlib._pylab_helpers import Gcf
73 if not fig_nums:
73 if not fig_nums:
74 fig_managers = Gcf.get_all_fig_managers()
74 fig_managers = Gcf.get_all_fig_managers()
75 return [fm.canvas.figure for fm in fig_managers]
75 return [fm.canvas.figure for fm in fig_managers]
76 else:
76 else:
77 figs = []
77 figs = []
78 for num in fig_nums:
78 for num in fig_nums:
79 f = Gcf.figs.get(num)
79 f = Gcf.figs.get(num)
80 if f is None:
80 if f is None:
81 print('Warning: figure %s not available.' % num)
81 print('Warning: figure %s not available.' % num)
82 else:
82 else:
83 figs.append(f.canvas.figure)
83 figs.append(f.canvas.figure)
84 return figs
84 return figs
85
85
86
86
87 def figsize(sizex, sizey):
87 def figsize(sizex, sizey):
88 """Set the default figure size to be [sizex, sizey].
88 """Set the default figure size to be [sizex, sizey].
89
89
90 This is just an easy to remember, convenience wrapper that sets::
90 This is just an easy to remember, convenience wrapper that sets::
91
91
92 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
92 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
93 """
93 """
94 import matplotlib
94 import matplotlib
95 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
95 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
96
96
97
97
98 def print_figure(fig, fmt='png', **kwargs):
98 def print_figure(fig, fmt='png', **kwargs):
99 """Print a figure to an image, and return the resulting bytes
99 """Print a figure to an image, and return the resulting bytes
100
100
101 Any extra keyword args are passed to fig.canvas.print_figure,
101 Any extra keyword args are passed to fig.canvas.print_figure,
102 such as ``quality`` or ``bbox_inches``.
102 such as ``quality`` or ``bbox_inches``.
103 """
103 """
104 from matplotlib import rcParams
104 from matplotlib import rcParams
105 # When there's an empty figure, we shouldn't return anything, otherwise we
105 # When there's an empty figure, we shouldn't return anything, otherwise we
106 # get big blank areas in the qt console.
106 # get big blank areas in the qt console.
107 if not fig.axes and not fig.lines:
107 if not fig.axes and not fig.lines:
108 return
108 return
109
109
110 dpi = rcParams['savefig.dpi']
110 dpi = rcParams['savefig.dpi']
111 if fmt == 'retina':
111 if fmt == 'retina':
112 dpi = dpi * 2
112 dpi = dpi * 2
113 fmt = 'png'
113 fmt = 'png'
114
114
115 # build keyword args
115 # build keyword args
116 kw = dict(
116 kw = dict(
117 format=fmt,
117 format=fmt,
118 fc=fig.get_facecolor(),
118 fc=fig.get_facecolor(),
119 ec=fig.get_edgecolor(),
119 ec=fig.get_edgecolor(),
120 dpi=dpi,
120 dpi=dpi,
121 )
121 )
122 # **kwargs get higher priority
122 # **kwargs get higher priority
123 kw.update(kwargs)
123 kw.update(kwargs)
124 print(kw)
125
124
126 bytes_io = BytesIO()
125 bytes_io = BytesIO()
127 fig.canvas.print_figure(bytes_io, **kw)
126 fig.canvas.print_figure(bytes_io, **kw)
128 return bytes_io.getvalue()
127 return bytes_io.getvalue()
129
128
130 def retina_figure(fig, **kwargs):
129 def retina_figure(fig, **kwargs):
131 """format a figure as a pixel-doubled (retina) PNG"""
130 """format a figure as a pixel-doubled (retina) PNG"""
132 pngdata = print_figure(fig, fmt='retina', **kwargs)
131 pngdata = print_figure(fig, fmt='retina', **kwargs)
133 w, h = _pngxy(pngdata)
132 w, h = _pngxy(pngdata)
134 metadata = dict(width=w//2, height=h//2)
133 metadata = dict(width=w//2, height=h//2)
135 return pngdata, metadata
134 return pngdata, metadata
136
135
137 # We need a little factory function here to create the closure where
136 # We need a little factory function here to create the closure where
138 # safe_execfile can live.
137 # safe_execfile can live.
139 def mpl_runner(safe_execfile):
138 def mpl_runner(safe_execfile):
140 """Factory to return a matplotlib-enabled runner for %run.
139 """Factory to return a matplotlib-enabled runner for %run.
141
140
142 Parameters
141 Parameters
143 ----------
142 ----------
144 safe_execfile : function
143 safe_execfile : function
145 This must be a function with the same interface as the
144 This must be a function with the same interface as the
146 :meth:`safe_execfile` method of IPython.
145 :meth:`safe_execfile` method of IPython.
147
146
148 Returns
147 Returns
149 -------
148 -------
150 A function suitable for use as the ``runner`` argument of the %run magic
149 A function suitable for use as the ``runner`` argument of the %run magic
151 function.
150 function.
152 """
151 """
153
152
154 def mpl_execfile(fname,*where,**kw):
153 def mpl_execfile(fname,*where,**kw):
155 """matplotlib-aware wrapper around safe_execfile.
154 """matplotlib-aware wrapper around safe_execfile.
156
155
157 Its interface is identical to that of the :func:`execfile` builtin.
156 Its interface is identical to that of the :func:`execfile` builtin.
158
157
159 This is ultimately a call to execfile(), but wrapped in safeties to
158 This is ultimately a call to execfile(), but wrapped in safeties to
160 properly handle interactive rendering."""
159 properly handle interactive rendering."""
161
160
162 import matplotlib
161 import matplotlib
163 import matplotlib.pylab as pylab
162 import matplotlib.pylab as pylab
164
163
165 #print '*** Matplotlib runner ***' # dbg
164 #print '*** Matplotlib runner ***' # dbg
166 # turn off rendering until end of script
165 # turn off rendering until end of script
167 is_interactive = matplotlib.rcParams['interactive']
166 is_interactive = matplotlib.rcParams['interactive']
168 matplotlib.interactive(False)
167 matplotlib.interactive(False)
169 safe_execfile(fname,*where,**kw)
168 safe_execfile(fname,*where,**kw)
170 matplotlib.interactive(is_interactive)
169 matplotlib.interactive(is_interactive)
171 # make rendering call now, if the user tried to do it
170 # make rendering call now, if the user tried to do it
172 if pylab.draw_if_interactive.called:
171 if pylab.draw_if_interactive.called:
173 pylab.draw()
172 pylab.draw()
174 pylab.draw_if_interactive.called = False
173 pylab.draw_if_interactive.called = False
175
174
176 return mpl_execfile
175 return mpl_execfile
177
176
178
177
179 def select_figure_formats(shell, formats, **kwargs):
178 def select_figure_formats(shell, formats, **kwargs):
180 """Select figure formats for the inline backend.
179 """Select figure formats for the inline backend.
181
180
182 Parameters
181 Parameters
183 ==========
182 ==========
184 shell : InteractiveShell
183 shell : InteractiveShell
185 The main IPython instance.
184 The main IPython instance.
186 formats : str or set
185 formats : str or set
187 One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
186 One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
188 **kwargs : any
187 **kwargs : any
189 Extra keyword arguments to be passed to fig.canvas.print_figure.
188 Extra keyword arguments to be passed to fig.canvas.print_figure.
190 """
189 """
191 from matplotlib.figure import Figure
190 from matplotlib.figure import Figure
192 from IPython.kernel.zmq.pylab import backend_inline
191 from IPython.kernel.zmq.pylab import backend_inline
193
192
194 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
193 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
195 png_formatter = shell.display_formatter.formatters['image/png']
194 png_formatter = shell.display_formatter.formatters['image/png']
196 jpg_formatter = shell.display_formatter.formatters['image/jpeg']
195 jpg_formatter = shell.display_formatter.formatters['image/jpeg']
197 pdf_formatter = shell.display_formatter.formatters['application/pdf']
196 pdf_formatter = shell.display_formatter.formatters['application/pdf']
198
197
199 if isinstance(formats, py3compat.string_types):
198 if isinstance(formats, py3compat.string_types):
200 formats = {formats}
199 formats = {formats}
201 # cast in case of list / tuple
200 # cast in case of list / tuple
202 formats = set(formats)
201 formats = set(formats)
203
202
204 [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ]
203 [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ]
205
204
206 supported = {'png', 'png2x', 'retina', 'jpg', 'jpeg', 'svg', 'pdf'}
205 supported = {'png', 'png2x', 'retina', 'jpg', 'jpeg', 'svg', 'pdf'}
207 bad = formats.difference(supported)
206 bad = formats.difference(supported)
208 if bad:
207 if bad:
209 s = "{%s}" % ",".join([repr(f) for f in bad])
208 s = "{%s}" % ",".join([repr(f) for f in bad])
210 raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', 'pdf' not %s" % s)
209 raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', 'pdf' not %s" % s)
211
210
212 if 'png' in formats:
211 if 'png' in formats:
213 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
212 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
214 if 'retina' in formats or 'png2x' in formats:
213 if 'retina' in formats or 'png2x' in formats:
215 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
214 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
216 if 'jpg' in formats or 'jpeg' in formats:
215 if 'jpg' in formats or 'jpeg' in formats:
217 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))
216 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))
218 if 'svg' in formats:
217 if 'svg' in formats:
219 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg', **kwargs))
218 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg', **kwargs))
220 if 'pdf' in formats:
219 if 'pdf' in formats:
221 pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf', **kwargs))
220 pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf', **kwargs))
222
221
223 #-----------------------------------------------------------------------------
222 #-----------------------------------------------------------------------------
224 # Code for initializing matplotlib and importing pylab
223 # Code for initializing matplotlib and importing pylab
225 #-----------------------------------------------------------------------------
224 #-----------------------------------------------------------------------------
226
225
227
226
228 def find_gui_and_backend(gui=None, gui_select=None):
227 def find_gui_and_backend(gui=None, gui_select=None):
229 """Given a gui string return the gui and mpl backend.
228 """Given a gui string return the gui and mpl backend.
230
229
231 Parameters
230 Parameters
232 ----------
231 ----------
233 gui : str
232 gui : str
234 Can be one of ('tk','gtk','wx','qt','qt4','inline').
233 Can be one of ('tk','gtk','wx','qt','qt4','inline').
235 gui_select : str
234 gui_select : str
236 Can be one of ('tk','gtk','wx','qt','qt4','inline').
235 Can be one of ('tk','gtk','wx','qt','qt4','inline').
237 This is any gui already selected by the shell.
236 This is any gui already selected by the shell.
238
237
239 Returns
238 Returns
240 -------
239 -------
241 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
240 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
242 'WXAgg','Qt4Agg','module://IPython.kernel.zmq.pylab.backend_inline').
241 'WXAgg','Qt4Agg','module://IPython.kernel.zmq.pylab.backend_inline').
243 """
242 """
244
243
245 import matplotlib
244 import matplotlib
246
245
247 if gui and gui != 'auto':
246 if gui and gui != 'auto':
248 # select backend based on requested gui
247 # select backend based on requested gui
249 backend = backends[gui]
248 backend = backends[gui]
250 else:
249 else:
251 # We need to read the backend from the original data structure, *not*
250 # We need to read the backend from the original data structure, *not*
252 # from mpl.rcParams, since a prior invocation of %matplotlib may have
251 # from mpl.rcParams, since a prior invocation of %matplotlib may have
253 # overwritten that.
252 # overwritten that.
254 # WARNING: this assumes matplotlib 1.1 or newer!!
253 # WARNING: this assumes matplotlib 1.1 or newer!!
255 backend = matplotlib.rcParamsOrig['backend']
254 backend = matplotlib.rcParamsOrig['backend']
256 # In this case, we need to find what the appropriate gui selection call
255 # In this case, we need to find what the appropriate gui selection call
257 # should be for IPython, so we can activate inputhook accordingly
256 # should be for IPython, so we can activate inputhook accordingly
258 gui = backend2gui.get(backend, None)
257 gui = backend2gui.get(backend, None)
259
258
260 # If we have already had a gui active, we need it and inline are the
259 # If we have already had a gui active, we need it and inline are the
261 # ones allowed.
260 # ones allowed.
262 if gui_select and gui != gui_select:
261 if gui_select and gui != gui_select:
263 gui = gui_select
262 gui = gui_select
264 backend = backends[gui]
263 backend = backends[gui]
265
264
266 return gui, backend
265 return gui, backend
267
266
268
267
269 def activate_matplotlib(backend):
268 def activate_matplotlib(backend):
270 """Activate the given backend and set interactive to True."""
269 """Activate the given backend and set interactive to True."""
271
270
272 import matplotlib
271 import matplotlib
273 matplotlib.interactive(True)
272 matplotlib.interactive(True)
274
273
275 # Matplotlib had a bug where even switch_backend could not force
274 # Matplotlib had a bug where even switch_backend could not force
276 # the rcParam to update. This needs to be set *before* the module
275 # the rcParam to update. This needs to be set *before* the module
277 # magic of switch_backend().
276 # magic of switch_backend().
278 matplotlib.rcParams['backend'] = backend
277 matplotlib.rcParams['backend'] = backend
279
278
280 import matplotlib.pyplot
279 import matplotlib.pyplot
281 matplotlib.pyplot.switch_backend(backend)
280 matplotlib.pyplot.switch_backend(backend)
282
281
283 # This must be imported last in the matplotlib series, after
282 # This must be imported last in the matplotlib series, after
284 # backend/interactivity choices have been made
283 # backend/interactivity choices have been made
285 import matplotlib.pylab as pylab
284 import matplotlib.pylab as pylab
286
285
287 pylab.show._needmain = False
286 pylab.show._needmain = False
288 # We need to detect at runtime whether show() is called by the user.
287 # We need to detect at runtime whether show() is called by the user.
289 # For this, we wrap it into a decorator which adds a 'called' flag.
288 # For this, we wrap it into a decorator which adds a 'called' flag.
290 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
289 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
291
290
292
291
293 def import_pylab(user_ns, import_all=True):
292 def import_pylab(user_ns, import_all=True):
294 """Populate the namespace with pylab-related values.
293 """Populate the namespace with pylab-related values.
295
294
296 Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
295 Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
297
296
298 Also imports a few names from IPython (figsize, display, getfigs)
297 Also imports a few names from IPython (figsize, display, getfigs)
299
298
300 """
299 """
301
300
302 # Import numpy as np/pyplot as plt are conventions we're trying to
301 # Import numpy as np/pyplot as plt are conventions we're trying to
303 # somewhat standardize on. Making them available to users by default
302 # somewhat standardize on. Making them available to users by default
304 # will greatly help this.
303 # will greatly help this.
305 s = ("import numpy\n"
304 s = ("import numpy\n"
306 "import matplotlib\n"
305 "import matplotlib\n"
307 "from matplotlib import pylab, mlab, pyplot\n"
306 "from matplotlib import pylab, mlab, pyplot\n"
308 "np = numpy\n"
307 "np = numpy\n"
309 "plt = pyplot\n"
308 "plt = pyplot\n"
310 )
309 )
311 exec(s, user_ns)
310 exec(s, user_ns)
312
311
313 if import_all:
312 if import_all:
314 s = ("from matplotlib.pylab import *\n"
313 s = ("from matplotlib.pylab import *\n"
315 "from numpy import *\n")
314 "from numpy import *\n")
316 exec(s, user_ns)
315 exec(s, user_ns)
317
316
318 # IPython symbols to add
317 # IPython symbols to add
319 user_ns['figsize'] = figsize
318 user_ns['figsize'] = figsize
320 from IPython.core.display import display
319 from IPython.core.display import display
321 # Add display and getfigs to the user's namespace
320 # Add display and getfigs to the user's namespace
322 user_ns['display'] = display
321 user_ns['display'] = display
323 user_ns['getfigs'] = getfigs
322 user_ns['getfigs'] = getfigs
324
323
325
324
326 def configure_inline_support(shell, backend):
325 def configure_inline_support(shell, backend):
327 """Configure an IPython shell object for matplotlib use.
326 """Configure an IPython shell object for matplotlib use.
328
327
329 Parameters
328 Parameters
330 ----------
329 ----------
331 shell : InteractiveShell instance
330 shell : InteractiveShell instance
332
331
333 backend : matplotlib backend
332 backend : matplotlib backend
334 """
333 """
335 # If using our svg payload backend, register the post-execution
334 # If using our svg payload backend, register the post-execution
336 # function that will pick up the results for display. This can only be
335 # function that will pick up the results for display. This can only be
337 # done with access to the real shell object.
336 # done with access to the real shell object.
338
337
339 # Note: if we can't load the inline backend, then there's no point
338 # Note: if we can't load the inline backend, then there's no point
340 # continuing (such as in terminal-only shells in environments without
339 # continuing (such as in terminal-only shells in environments without
341 # zeromq available).
340 # zeromq available).
342 try:
341 try:
343 from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
342 from IPython.kernel.zmq.pylab.backend_inline import InlineBackend
344 except ImportError:
343 except ImportError:
345 return
344 return
346 from matplotlib import pyplot
345 from matplotlib import pyplot
347
346
348 cfg = InlineBackend.instance(parent=shell)
347 cfg = InlineBackend.instance(parent=shell)
349 cfg.shell = shell
348 cfg.shell = shell
350 if cfg not in shell.configurables:
349 if cfg not in shell.configurables:
351 shell.configurables.append(cfg)
350 shell.configurables.append(cfg)
352
351
353 if backend == backends['inline']:
352 if backend == backends['inline']:
354 from IPython.kernel.zmq.pylab.backend_inline import flush_figures
353 from IPython.kernel.zmq.pylab.backend_inline import flush_figures
355 shell.register_post_execute(flush_figures)
354 shell.register_post_execute(flush_figures)
356
355
357 # Save rcParams that will be overwrittern
356 # Save rcParams that will be overwrittern
358 shell._saved_rcParams = dict()
357 shell._saved_rcParams = dict()
359 for k in cfg.rc:
358 for k in cfg.rc:
360 shell._saved_rcParams[k] = pyplot.rcParams[k]
359 shell._saved_rcParams[k] = pyplot.rcParams[k]
361 # load inline_rc
360 # load inline_rc
362 pyplot.rcParams.update(cfg.rc)
361 pyplot.rcParams.update(cfg.rc)
363 else:
362 else:
364 from IPython.kernel.zmq.pylab.backend_inline import flush_figures
363 from IPython.kernel.zmq.pylab.backend_inline import flush_figures
365 if flush_figures in shell._post_execute:
364 if flush_figures in shell._post_execute:
366 shell._post_execute.pop(flush_figures)
365 shell._post_execute.pop(flush_figures)
367 if hasattr(shell, '_saved_rcParams'):
366 if hasattr(shell, '_saved_rcParams'):
368 pyplot.rcParams.update(shell._saved_rcParams)
367 pyplot.rcParams.update(shell._saved_rcParams)
369 del shell._saved_rcParams
368 del shell._saved_rcParams
370
369
371 # Setup the default figure format
370 # Setup the default figure format
372 select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
371 select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
373
372
General Comments 0
You need to be logged in to leave comments. Login now