Show More
@@ -1,142 +1,143 b'' | |||||
1 | """Implementation of magic functions for matplotlib/pylab support. |
|
1 | """Implementation of magic functions for matplotlib/pylab support. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2012 The IPython Development Team. |
|
4 | # Copyright (c) 2012 The IPython Development Team. | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the Modified BSD License. |
|
6 | # Distributed under the terms of the Modified BSD License. | |
7 | # |
|
7 | # | |
8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | # Our own packages |
|
15 | # Our own packages | |
16 | from IPython.config.application import Application |
|
16 | from IPython.config.application import Application | |
17 | from IPython.core import magic_arguments |
|
17 | from IPython.core import magic_arguments | |
18 | from IPython.core.magic import Magics, magics_class, line_magic |
|
18 | from IPython.core.magic import Magics, magics_class, line_magic | |
19 | from IPython.testing.skipdoctest import skip_doctest |
|
19 | from IPython.testing.skipdoctest import skip_doctest | |
20 | from IPython.utils.warn import warn |
|
20 | from IPython.utils.warn import warn | |
21 | from IPython.core.pylabtools import backends |
|
21 | from IPython.core.pylabtools import backends | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Magic implementation classes |
|
24 | # Magic implementation classes | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | magic_gui_arg = magic_arguments.argument( |
|
27 | magic_gui_arg = magic_arguments.argument( | |
28 | 'gui', nargs='?', |
|
28 | 'gui', nargs='?', | |
29 | help="""Name of the matplotlib backend to use %s. |
|
29 | help="""Name of the matplotlib backend to use %s. | |
30 | If given, the corresponding matplotlib backend is used, |
|
30 | If given, the corresponding matplotlib backend is used, | |
31 | otherwise it will be matplotlib's default |
|
31 | otherwise it will be matplotlib's default | |
32 | (which you can set in your matplotlib config file). |
|
32 | (which you can set in your matplotlib config file). | |
33 | """ % str(tuple(sorted(backends.keys()))) |
|
33 | """ % str(tuple(sorted(backends.keys()))) | |
34 | ) |
|
34 | ) | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | @magics_class |
|
37 | @magics_class | |
38 | class PylabMagics(Magics): |
|
38 | class PylabMagics(Magics): | |
39 | """Magics related to matplotlib's pylab support""" |
|
39 | """Magics related to matplotlib's pylab support""" | |
40 |
|
40 | |||
41 | @skip_doctest |
|
41 | @skip_doctest | |
42 | @line_magic |
|
42 | @line_magic | |
43 | @magic_arguments.magic_arguments() |
|
43 | @magic_arguments.magic_arguments() | |
44 | @magic_gui_arg |
|
44 | @magic_gui_arg | |
45 | def matplotlib(self, line=''): |
|
45 | def matplotlib(self, line=''): | |
46 | """Set up matplotlib to work interactively. |
|
46 | """Set up matplotlib to work interactively. | |
47 |
|
47 | |||
48 | This function lets you activate matplotlib interactive support |
|
48 | This function lets you activate matplotlib interactive support | |
49 | at any point during an IPython session. |
|
49 | at any point during an IPython session. | |
50 | It does not import anything into the interactive namespace. |
|
50 | It does not import anything into the interactive namespace. | |
51 |
|
51 | |||
52 | If you are using the inline matplotlib backend for embedded figures, |
|
52 | If you are using the inline matplotlib backend for embedded figures, | |
53 | you can adjust its behavior via the %config magic:: |
|
53 | you can adjust its behavior via the %config magic:: | |
54 |
|
54 | |||
55 | # enable SVG figures, necessary for SVG+XHTML export in the qtconsole |
|
55 | # enable SVG figures, necessary for SVG+XHTML export in the qtconsole | |
56 | In [1]: %config InlineBackend.figure_format = 'svg' |
|
56 | In [1]: %config InlineBackend.figure_format = 'svg' | |
57 |
|
57 | |||
58 | # change the behavior of closing all figures at the end of each |
|
58 | # change the behavior of closing all figures at the end of each | |
59 | # execution (cell), or allowing reuse of active figures across |
|
59 | # execution (cell), or allowing reuse of active figures across | |
60 | # cells: |
|
60 | # cells: | |
61 | In [2]: %config InlineBackend.close_figures = False |
|
61 | In [2]: %config InlineBackend.close_figures = False | |
62 |
|
62 | |||
63 | Examples |
|
63 | Examples | |
64 | -------- |
|
64 | -------- | |
65 | In this case, where the MPL default is TkAgg:: |
|
65 | In this case, where the MPL default is TkAgg:: | |
66 |
|
66 | |||
67 | In [2]: %matplotlib |
|
67 | In [2]: %matplotlib | |
68 | Using matplotlib backend: TkAgg |
|
68 | Using matplotlib backend: TkAgg | |
69 |
|
69 | |||
70 | But you can explicitly request a different backend:: |
|
70 | But you can explicitly request a different backend:: | |
71 |
|
71 | |||
72 | In [3]: %matplotlib qt |
|
72 | In [3]: %matplotlib qt | |
73 | """ |
|
73 | """ | |
74 | args = magic_arguments.parse_argstring(self.matplotlib, line) |
|
74 | args = magic_arguments.parse_argstring(self.matplotlib, line) | |
75 | gui, backend = self.shell.enable_matplotlib(args.gui) |
|
75 | gui, backend = self.shell.enable_matplotlib(args.gui) | |
76 | self._show_matplotlib_backend(args.gui, backend) |
|
76 | self._show_matplotlib_backend(args.gui, backend) | |
77 |
|
77 | |||
78 | @skip_doctest |
|
78 | @skip_doctest | |
79 | @line_magic |
|
79 | @line_magic | |
80 | @magic_arguments.magic_arguments() |
|
80 | @magic_arguments.magic_arguments() | |
81 | @magic_arguments.argument( |
|
81 | @magic_arguments.argument( | |
82 | '--no-import-all', action='store_true', default=None, |
|
82 | '--no-import-all', action='store_true', default=None, | |
83 | help="""Prevent IPython from performing ``import *`` into the interactive namespace. |
|
83 | help="""Prevent IPython from performing ``import *`` into the interactive namespace. | |
84 |
|
84 | |||
85 | The names that will still be added to the namespace if this flag is given:: |
|
85 | You can govern the default behavior of this flag with the | |
86 |
|
||||
87 | numpy |
|
|||
88 | matplotlib |
|
|||
89 | np (numpy alias) |
|
|||
90 | plt (matplotlib.pyplot alias) |
|
|||
91 | pylab (from matplotlib) |
|
|||
92 | pyplot (from matplotlib) |
|
|||
93 | mlab (from matplotlib) |
|
|||
94 | display (from IPython) |
|
|||
95 | figsize (from IPython) |
|
|||
96 | getfigs (from IPython) |
|
|||
97 |
|
||||
98 | You can govern the default behavior with the |
|
|||
99 | InteractiveShellApp.pylab_import_all configurable. |
|
86 | InteractiveShellApp.pylab_import_all configurable. | |
100 | """ |
|
87 | """ | |
101 | ) |
|
88 | ) | |
102 | @magic_gui_arg |
|
89 | @magic_gui_arg | |
103 | def pylab(self, line=''): |
|
90 | def pylab(self, line=''): | |
104 | """Load numpy and matplotlib to work interactively. |
|
91 | """Load numpy and matplotlib to work interactively. | |
105 |
|
92 | |||
106 | This function lets you activate pylab (matplotlib, numpy and |
|
93 | This function lets you activate pylab (matplotlib, numpy and | |
107 | interactive support) at any point during an IPython session. |
|
94 | interactive support) at any point during an IPython session. | |
108 |
|
95 | |||
109 | It will import at the top level numpy as np, pyplot as plt, matplotlib, |
|
96 | %pylab makes the following imports:: | |
110 | pylab and mlab, as well as all names from numpy and pylab. |
|
|||
111 |
|
97 | |||
112 | See the %matplotlib magic for more details. |
|
98 | import numpy | |
|
99 | import matplotlib | |||
|
100 | from matplotlib import pylab, mlab, pyplot | |||
|
101 | np = numpy | |||
|
102 | plt = pyplot | |||
|
103 | ||||
|
104 | from IPython.display import display | |||
|
105 | from IPython.core.pylabtools import figsize, getfigs | |||
|
106 | ||||
|
107 | from pylab import * | |||
|
108 | from numpy import * | |||
|
109 | ||||
|
110 | If you pass `--no-import-all`, the last two `*` imports will be excluded. | |||
|
111 | ||||
|
112 | See the %matplotlib magic for more details about activating matplotlib | |||
|
113 | without affecting the interactive namespace. | |||
113 | """ |
|
114 | """ | |
114 | args = magic_arguments.parse_argstring(self.pylab, line) |
|
115 | args = magic_arguments.parse_argstring(self.pylab, line) | |
115 | if args.no_import_all is None: |
|
116 | if args.no_import_all is None: | |
116 | # get default from Application |
|
117 | # get default from Application | |
117 | if Application.initialized(): |
|
118 | if Application.initialized(): | |
118 | app = Application.instance() |
|
119 | app = Application.instance() | |
119 | try: |
|
120 | try: | |
120 | import_all = app.pylab_import_all |
|
121 | import_all = app.pylab_import_all | |
121 | except AttributeError: |
|
122 | except AttributeError: | |
122 | import_all = True |
|
123 | import_all = True | |
123 | else: |
|
124 | else: | |
124 | # nothing specified, no app - default True |
|
125 | # nothing specified, no app - default True | |
125 | import_all = True |
|
126 | import_all = True | |
126 | else: |
|
127 | else: | |
127 | # invert no-import flag |
|
128 | # invert no-import flag | |
128 | import_all = not args.no_import_all |
|
129 | import_all = not args.no_import_all | |
129 |
|
130 | |||
130 | gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all) |
|
131 | gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all) | |
131 | self._show_matplotlib_backend(args.gui, backend) |
|
132 | self._show_matplotlib_backend(args.gui, backend) | |
132 | print ("Populating the interactive namespace from numpy and matplotlib") |
|
133 | print ("Populating the interactive namespace from numpy and matplotlib") | |
133 | if clobbered: |
|
134 | if clobbered: | |
134 | warn("pylab import has clobbered these variables: %s" % clobbered + |
|
135 | warn("pylab import has clobbered these variables: %s" % clobbered + | |
135 | "\n`%pylab --no-import-all` prevents importing * from pylab and numpy" |
|
136 | "\n`%pylab --no-import-all` prevents importing * from pylab and numpy" | |
136 | ) |
|
137 | ) | |
137 |
|
138 | |||
138 | def _show_matplotlib_backend(self, gui, backend): |
|
139 | def _show_matplotlib_backend(self, gui, backend): | |
139 | """show matplotlib message backend message""" |
|
140 | """show matplotlib message backend message""" | |
140 | if not gui or gui == 'auto': |
|
141 | if not gui or gui == 'auto': | |
141 | print ("Using matplotlib backend: %s" % backend) |
|
142 | print ("Using matplotlib backend: %s" % backend) | |
142 |
|
143 |
General Comments 0
You need to be logged in to leave comments.
Login now