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