Show More
@@ -1,356 +1,358 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', quality=90): |
|
98 | def print_figure(fig, fmt='png', quality=90): | |
99 | """Convert a figure to svg, png or jpg for inline display. |
|
99 | """Convert a figure to svg, png or jpg for inline display. | |
100 | Quality is only relevant for jpg. |
|
100 | Quality is only relevant for jpg. | |
101 | """ |
|
101 | """ | |
102 | from matplotlib import rcParams |
|
102 | from matplotlib import rcParams | |
103 | # When there's an empty figure, we shouldn't return anything, otherwise we |
|
103 | # When there's an empty figure, we shouldn't return anything, otherwise we | |
104 | # get big blank areas in the qt console. |
|
104 | # get big blank areas in the qt console. | |
105 | if not fig.axes and not fig.lines: |
|
105 | if not fig.axes and not fig.lines: | |
106 | return |
|
106 | return | |
107 |
|
107 | |||
108 | fc = fig.get_facecolor() |
|
108 | fc = fig.get_facecolor() | |
109 | ec = fig.get_edgecolor() |
|
109 | ec = fig.get_edgecolor() | |
110 | bytes_io = BytesIO() |
|
110 | bytes_io = BytesIO() | |
111 | dpi = rcParams['savefig.dpi'] |
|
111 | dpi = rcParams['savefig.dpi'] | |
112 | if fmt == 'retina': |
|
112 | if fmt == 'retina': | |
113 | dpi = dpi * 2 |
|
113 | dpi = dpi * 2 | |
114 | fmt = 'png' |
|
114 | fmt = 'png' | |
115 | fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight', |
|
115 | fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight', | |
116 | facecolor=fc, edgecolor=ec, dpi=dpi, quality=quality) |
|
116 | facecolor=fc, edgecolor=ec, dpi=dpi, quality=quality) | |
117 | data = bytes_io.getvalue() |
|
117 | data = bytes_io.getvalue() | |
118 | return data |
|
118 | return data | |
119 |
|
119 | |||
120 | def retina_figure(fig): |
|
120 | def retina_figure(fig): | |
121 | """format a figure as a pixel-doubled (retina) PNG""" |
|
121 | """format a figure as a pixel-doubled (retina) PNG""" | |
122 | pngdata = print_figure(fig, fmt='retina') |
|
122 | pngdata = print_figure(fig, fmt='retina') | |
123 | w, h = _pngxy(pngdata) |
|
123 | w, h = _pngxy(pngdata) | |
124 | metadata = dict(width=w//2, height=h//2) |
|
124 | metadata = dict(width=w//2, height=h//2) | |
125 | return pngdata, metadata |
|
125 | return pngdata, metadata | |
126 |
|
126 | |||
127 | # We need a little factory function here to create the closure where |
|
127 | # We need a little factory function here to create the closure where | |
128 | # safe_execfile can live. |
|
128 | # safe_execfile can live. | |
129 | def mpl_runner(safe_execfile): |
|
129 | def mpl_runner(safe_execfile): | |
130 | """Factory to return a matplotlib-enabled runner for %run. |
|
130 | """Factory to return a matplotlib-enabled runner for %run. | |
131 |
|
131 | |||
132 | Parameters |
|
132 | Parameters | |
133 | ---------- |
|
133 | ---------- | |
134 | safe_execfile : function |
|
134 | safe_execfile : function | |
135 | This must be a function with the same interface as the |
|
135 | This must be a function with the same interface as the | |
136 | :meth:`safe_execfile` method of IPython. |
|
136 | :meth:`safe_execfile` method of IPython. | |
137 |
|
137 | |||
138 | Returns |
|
138 | Returns | |
139 | ------- |
|
139 | ------- | |
140 | A function suitable for use as the ``runner`` argument of the %run magic |
|
140 | A function suitable for use as the ``runner`` argument of the %run magic | |
141 | function. |
|
141 | function. | |
142 | """ |
|
142 | """ | |
143 |
|
143 | |||
144 | def mpl_execfile(fname,*where,**kw): |
|
144 | def mpl_execfile(fname,*where,**kw): | |
145 | """matplotlib-aware wrapper around safe_execfile. |
|
145 | """matplotlib-aware wrapper around safe_execfile. | |
146 |
|
146 | |||
147 | Its interface is identical to that of the :func:`execfile` builtin. |
|
147 | Its interface is identical to that of the :func:`execfile` builtin. | |
148 |
|
148 | |||
149 | This is ultimately a call to execfile(), but wrapped in safeties to |
|
149 | This is ultimately a call to execfile(), but wrapped in safeties to | |
150 | properly handle interactive rendering.""" |
|
150 | properly handle interactive rendering.""" | |
151 |
|
151 | |||
152 | import matplotlib |
|
152 | import matplotlib | |
153 | import matplotlib.pylab as pylab |
|
153 | import matplotlib.pylab as pylab | |
154 |
|
154 | |||
155 | #print '*** Matplotlib runner ***' # dbg |
|
155 | #print '*** Matplotlib runner ***' # dbg | |
156 | # turn off rendering until end of script |
|
156 | # turn off rendering until end of script | |
157 | is_interactive = matplotlib.rcParams['interactive'] |
|
157 | is_interactive = matplotlib.rcParams['interactive'] | |
158 | matplotlib.interactive(False) |
|
158 | matplotlib.interactive(False) | |
159 | safe_execfile(fname,*where,**kw) |
|
159 | safe_execfile(fname,*where,**kw) | |
160 | matplotlib.interactive(is_interactive) |
|
160 | matplotlib.interactive(is_interactive) | |
161 | # make rendering call now, if the user tried to do it |
|
161 | # make rendering call now, if the user tried to do it | |
162 | if pylab.draw_if_interactive.called: |
|
162 | if pylab.draw_if_interactive.called: | |
163 | pylab.draw() |
|
163 | pylab.draw() | |
164 | pylab.draw_if_interactive.called = False |
|
164 | pylab.draw_if_interactive.called = False | |
165 |
|
165 | |||
166 | return mpl_execfile |
|
166 | return mpl_execfile | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | def select_figure_formats(shell, formats, quality=90): |
|
169 | def select_figure_formats(shell, formats, quality=90): | |
170 | """Select figure formats for the inline backend. |
|
170 | """Select figure formats for the inline backend. | |
171 |
|
171 | |||
172 | Parameters |
|
172 | Parameters | |
173 | ========== |
|
173 | ========== | |
174 | shell : InteractiveShell |
|
174 | shell : InteractiveShell | |
175 | The main IPython instance |
|
175 | The main IPython instance. | |
176 | formats : list |
|
176 | formats : list | |
177 | One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'. |
|
177 | One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'. | |
|
178 | quality : int | |||
|
179 | A percentage for the quality of JPEG figures. | |||
178 | """ |
|
180 | """ | |
179 | from matplotlib.figure import Figure |
|
181 | from matplotlib.figure import Figure | |
180 | from IPython.kernel.zmq.pylab import backend_inline |
|
182 | from IPython.kernel.zmq.pylab import backend_inline | |
181 |
|
183 | |||
182 | svg_formatter = shell.display_formatter.formatters['image/svg+xml'] |
|
184 | svg_formatter = shell.display_formatter.formatters['image/svg+xml'] | |
183 | png_formatter = shell.display_formatter.formatters['image/png'] |
|
185 | png_formatter = shell.display_formatter.formatters['image/png'] | |
184 | jpg_formatter = shell.display_formatter.formatters['image/jpeg'] |
|
186 | jpg_formatter = shell.display_formatter.formatters['image/jpeg'] | |
185 | pdf_formatter = shell.display_formatter.formatters['application/pdf'] |
|
187 | pdf_formatter = shell.display_formatter.formatters['application/pdf'] | |
186 |
|
188 | |||
187 | if isinstance(formats, py3compat.string_types): |
|
189 | if isinstance(formats, py3compat.string_types): | |
188 | formats = {formats} |
|
190 | formats = {formats} | |
189 |
|
191 | |||
190 | [ f.type_printers.pop(Figure, None) for f in {svg_formatter, png_formatter, jpg_formatter} ] |
|
192 | [ f.type_printers.pop(Figure, None) for f in {svg_formatter, png_formatter, jpg_formatter} ] | |
191 |
|
193 | |||
192 | for fmt in formats: |
|
194 | for fmt in formats: | |
193 | if fmt == 'png': |
|
195 | if fmt == 'png': | |
194 | png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png')) |
|
196 | png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png')) | |
195 | elif fmt in ('png2x', 'retina'): |
|
197 | elif fmt in ('png2x', 'retina'): | |
196 | png_formatter.for_type(Figure, retina_figure) |
|
198 | png_formatter.for_type(Figure, retina_figure) | |
197 | elif fmt in ('jpg', 'jpeg'): |
|
199 | elif fmt in ('jpg', 'jpeg'): | |
198 | jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', quality)) |
|
200 | jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', quality)) | |
199 | elif fmt == 'svg': |
|
201 | elif fmt == 'svg': | |
200 | svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg')) |
|
202 | svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg')) | |
201 | elif fmt == 'pdf': |
|
203 | elif fmt == 'pdf': | |
202 | pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf')) |
|
204 | pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf')) | |
203 | else: |
|
205 | else: | |
204 | raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', 'pdf' not %r" % fmt) |
|
206 | raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', 'pdf' not %r" % fmt) | |
205 |
|
207 | |||
206 | #----------------------------------------------------------------------------- |
|
208 | #----------------------------------------------------------------------------- | |
207 | # Code for initializing matplotlib and importing pylab |
|
209 | # Code for initializing matplotlib and importing pylab | |
208 | #----------------------------------------------------------------------------- |
|
210 | #----------------------------------------------------------------------------- | |
209 |
|
211 | |||
210 |
|
212 | |||
211 | def find_gui_and_backend(gui=None, gui_select=None): |
|
213 | def find_gui_and_backend(gui=None, gui_select=None): | |
212 | """Given a gui string return the gui and mpl backend. |
|
214 | """Given a gui string return the gui and mpl backend. | |
213 |
|
215 | |||
214 | Parameters |
|
216 | Parameters | |
215 | ---------- |
|
217 | ---------- | |
216 | gui : str |
|
218 | gui : str | |
217 | Can be one of ('tk','gtk','wx','qt','qt4','inline'). |
|
219 | Can be one of ('tk','gtk','wx','qt','qt4','inline'). | |
218 | gui_select : str |
|
220 | gui_select : str | |
219 | Can be one of ('tk','gtk','wx','qt','qt4','inline'). |
|
221 | Can be one of ('tk','gtk','wx','qt','qt4','inline'). | |
220 | This is any gui already selected by the shell. |
|
222 | This is any gui already selected by the shell. | |
221 |
|
223 | |||
222 | Returns |
|
224 | Returns | |
223 | ------- |
|
225 | ------- | |
224 | A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg', |
|
226 | A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg', | |
225 | 'WXAgg','Qt4Agg','module://IPython.kernel.zmq.pylab.backend_inline'). |
|
227 | 'WXAgg','Qt4Agg','module://IPython.kernel.zmq.pylab.backend_inline'). | |
226 | """ |
|
228 | """ | |
227 |
|
229 | |||
228 | import matplotlib |
|
230 | import matplotlib | |
229 |
|
231 | |||
230 | if gui and gui != 'auto': |
|
232 | if gui and gui != 'auto': | |
231 | # select backend based on requested gui |
|
233 | # select backend based on requested gui | |
232 | backend = backends[gui] |
|
234 | backend = backends[gui] | |
233 | else: |
|
235 | else: | |
234 | # We need to read the backend from the original data structure, *not* |
|
236 | # We need to read the backend from the original data structure, *not* | |
235 | # from mpl.rcParams, since a prior invocation of %matplotlib may have |
|
237 | # from mpl.rcParams, since a prior invocation of %matplotlib may have | |
236 | # overwritten that. |
|
238 | # overwritten that. | |
237 | # WARNING: this assumes matplotlib 1.1 or newer!! |
|
239 | # WARNING: this assumes matplotlib 1.1 or newer!! | |
238 | backend = matplotlib.rcParamsOrig['backend'] |
|
240 | backend = matplotlib.rcParamsOrig['backend'] | |
239 | # In this case, we need to find what the appropriate gui selection call |
|
241 | # In this case, we need to find what the appropriate gui selection call | |
240 | # should be for IPython, so we can activate inputhook accordingly |
|
242 | # should be for IPython, so we can activate inputhook accordingly | |
241 | gui = backend2gui.get(backend, None) |
|
243 | gui = backend2gui.get(backend, None) | |
242 |
|
244 | |||
243 | # If we have already had a gui active, we need it and inline are the |
|
245 | # If we have already had a gui active, we need it and inline are the | |
244 | # ones allowed. |
|
246 | # ones allowed. | |
245 | if gui_select and gui != gui_select: |
|
247 | if gui_select and gui != gui_select: | |
246 | gui = gui_select |
|
248 | gui = gui_select | |
247 | backend = backends[gui] |
|
249 | backend = backends[gui] | |
248 |
|
250 | |||
249 | return gui, backend |
|
251 | return gui, backend | |
250 |
|
252 | |||
251 |
|
253 | |||
252 | def activate_matplotlib(backend): |
|
254 | def activate_matplotlib(backend): | |
253 | """Activate the given backend and set interactive to True.""" |
|
255 | """Activate the given backend and set interactive to True.""" | |
254 |
|
256 | |||
255 | import matplotlib |
|
257 | import matplotlib | |
256 | matplotlib.interactive(True) |
|
258 | matplotlib.interactive(True) | |
257 |
|
259 | |||
258 | # Matplotlib had a bug where even switch_backend could not force |
|
260 | # Matplotlib had a bug where even switch_backend could not force | |
259 | # the rcParam to update. This needs to be set *before* the module |
|
261 | # the rcParam to update. This needs to be set *before* the module | |
260 | # magic of switch_backend(). |
|
262 | # magic of switch_backend(). | |
261 | matplotlib.rcParams['backend'] = backend |
|
263 | matplotlib.rcParams['backend'] = backend | |
262 |
|
264 | |||
263 | import matplotlib.pyplot |
|
265 | import matplotlib.pyplot | |
264 | matplotlib.pyplot.switch_backend(backend) |
|
266 | matplotlib.pyplot.switch_backend(backend) | |
265 |
|
267 | |||
266 | # This must be imported last in the matplotlib series, after |
|
268 | # This must be imported last in the matplotlib series, after | |
267 | # backend/interactivity choices have been made |
|
269 | # backend/interactivity choices have been made | |
268 | import matplotlib.pylab as pylab |
|
270 | import matplotlib.pylab as pylab | |
269 |
|
271 | |||
270 | pylab.show._needmain = False |
|
272 | pylab.show._needmain = False | |
271 | # We need to detect at runtime whether show() is called by the user. |
|
273 | # We need to detect at runtime whether show() is called by the user. | |
272 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
274 | # For this, we wrap it into a decorator which adds a 'called' flag. | |
273 | pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive) |
|
275 | pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive) | |
274 |
|
276 | |||
275 |
|
277 | |||
276 | def import_pylab(user_ns, import_all=True): |
|
278 | def import_pylab(user_ns, import_all=True): | |
277 | """Populate the namespace with pylab-related values. |
|
279 | """Populate the namespace with pylab-related values. | |
278 |
|
280 | |||
279 | Imports matplotlib, pylab, numpy, and everything from pylab and numpy. |
|
281 | Imports matplotlib, pylab, numpy, and everything from pylab and numpy. | |
280 |
|
282 | |||
281 | Also imports a few names from IPython (figsize, display, getfigs) |
|
283 | Also imports a few names from IPython (figsize, display, getfigs) | |
282 |
|
284 | |||
283 | """ |
|
285 | """ | |
284 |
|
286 | |||
285 | # Import numpy as np/pyplot as plt are conventions we're trying to |
|
287 | # Import numpy as np/pyplot as plt are conventions we're trying to | |
286 | # somewhat standardize on. Making them available to users by default |
|
288 | # somewhat standardize on. Making them available to users by default | |
287 | # will greatly help this. |
|
289 | # will greatly help this. | |
288 | s = ("import numpy\n" |
|
290 | s = ("import numpy\n" | |
289 | "import matplotlib\n" |
|
291 | "import matplotlib\n" | |
290 | "from matplotlib import pylab, mlab, pyplot\n" |
|
292 | "from matplotlib import pylab, mlab, pyplot\n" | |
291 | "np = numpy\n" |
|
293 | "np = numpy\n" | |
292 | "plt = pyplot\n" |
|
294 | "plt = pyplot\n" | |
293 | ) |
|
295 | ) | |
294 | exec(s, user_ns) |
|
296 | exec(s, user_ns) | |
295 |
|
297 | |||
296 | if import_all: |
|
298 | if import_all: | |
297 | s = ("from matplotlib.pylab import *\n" |
|
299 | s = ("from matplotlib.pylab import *\n" | |
298 | "from numpy import *\n") |
|
300 | "from numpy import *\n") | |
299 | exec(s, user_ns) |
|
301 | exec(s, user_ns) | |
300 |
|
302 | |||
301 | # IPython symbols to add |
|
303 | # IPython symbols to add | |
302 | user_ns['figsize'] = figsize |
|
304 | user_ns['figsize'] = figsize | |
303 | from IPython.core.display import display |
|
305 | from IPython.core.display import display | |
304 | # Add display and getfigs to the user's namespace |
|
306 | # Add display and getfigs to the user's namespace | |
305 | user_ns['display'] = display |
|
307 | user_ns['display'] = display | |
306 | user_ns['getfigs'] = getfigs |
|
308 | user_ns['getfigs'] = getfigs | |
307 |
|
309 | |||
308 |
|
310 | |||
309 | def configure_inline_support(shell, backend): |
|
311 | def configure_inline_support(shell, backend): | |
310 | """Configure an IPython shell object for matplotlib use. |
|
312 | """Configure an IPython shell object for matplotlib use. | |
311 |
|
313 | |||
312 | Parameters |
|
314 | Parameters | |
313 | ---------- |
|
315 | ---------- | |
314 | shell : InteractiveShell instance |
|
316 | shell : InteractiveShell instance | |
315 |
|
317 | |||
316 | backend : matplotlib backend |
|
318 | backend : matplotlib backend | |
317 | """ |
|
319 | """ | |
318 | # If using our svg payload backend, register the post-execution |
|
320 | # If using our svg payload backend, register the post-execution | |
319 | # function that will pick up the results for display. This can only be |
|
321 | # function that will pick up the results for display. This can only be | |
320 | # done with access to the real shell object. |
|
322 | # done with access to the real shell object. | |
321 |
|
323 | |||
322 | # Note: if we can't load the inline backend, then there's no point |
|
324 | # Note: if we can't load the inline backend, then there's no point | |
323 | # continuing (such as in terminal-only shells in environments without |
|
325 | # continuing (such as in terminal-only shells in environments without | |
324 | # zeromq available). |
|
326 | # zeromq available). | |
325 | try: |
|
327 | try: | |
326 | from IPython.kernel.zmq.pylab.backend_inline import InlineBackend |
|
328 | from IPython.kernel.zmq.pylab.backend_inline import InlineBackend | |
327 | except ImportError: |
|
329 | except ImportError: | |
328 | return |
|
330 | return | |
329 | from matplotlib import pyplot |
|
331 | from matplotlib import pyplot | |
330 |
|
332 | |||
331 | cfg = InlineBackend.instance(parent=shell) |
|
333 | cfg = InlineBackend.instance(parent=shell) | |
332 | cfg.shell = shell |
|
334 | cfg.shell = shell | |
333 | if cfg not in shell.configurables: |
|
335 | if cfg not in shell.configurables: | |
334 | shell.configurables.append(cfg) |
|
336 | shell.configurables.append(cfg) | |
335 |
|
337 | |||
336 | if backend == backends['inline']: |
|
338 | if backend == backends['inline']: | |
337 | from IPython.kernel.zmq.pylab.backend_inline import flush_figures |
|
339 | from IPython.kernel.zmq.pylab.backend_inline import flush_figures | |
338 | shell.register_post_execute(flush_figures) |
|
340 | shell.register_post_execute(flush_figures) | |
339 |
|
341 | |||
340 | # Save rcParams that will be overwrittern |
|
342 | # Save rcParams that will be overwrittern | |
341 | shell._saved_rcParams = dict() |
|
343 | shell._saved_rcParams = dict() | |
342 | for k in cfg.rc: |
|
344 | for k in cfg.rc: | |
343 | shell._saved_rcParams[k] = pyplot.rcParams[k] |
|
345 | shell._saved_rcParams[k] = pyplot.rcParams[k] | |
344 | # load inline_rc |
|
346 | # load inline_rc | |
345 | pyplot.rcParams.update(cfg.rc) |
|
347 | pyplot.rcParams.update(cfg.rc) | |
346 | else: |
|
348 | else: | |
347 | from IPython.kernel.zmq.pylab.backend_inline import flush_figures |
|
349 | from IPython.kernel.zmq.pylab.backend_inline import flush_figures | |
348 | if flush_figures in shell._post_execute: |
|
350 | if flush_figures in shell._post_execute: | |
349 | shell._post_execute.pop(flush_figures) |
|
351 | shell._post_execute.pop(flush_figures) | |
350 | if hasattr(shell, '_saved_rcParams'): |
|
352 | if hasattr(shell, '_saved_rcParams'): | |
351 | pyplot.rcParams.update(shell._saved_rcParams) |
|
353 | pyplot.rcParams.update(shell._saved_rcParams) | |
352 | del shell._saved_rcParams |
|
354 | del shell._saved_rcParams | |
353 |
|
355 | |||
354 | # Setup the default figure format |
|
356 | # Setup the default figure format | |
355 | select_figure_formats(shell, cfg.figure_formats, cfg.quality) |
|
357 | select_figure_formats(shell, cfg.figure_formats, cfg.quality) | |
356 |
|
358 |
General Comments 0
You need to be logged in to leave comments.
Login now