Show More
@@ -1,163 +1,167 b'' | |||||
1 | """Implementation of magic functions for matplotlib/pylab support. |
|
1 | """Implementation of magic functions for matplotlib/pylab support. | |
2 | """ |
|
2 | """ | |
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | # Copyright (c) 2012 The IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | # Our own packages |
|
16 | # Our own packages | |
17 | from traitlets.config.application import Application |
|
17 | from traitlets.config.application import Application | |
18 | from IPython.core import magic_arguments |
|
18 | from IPython.core import magic_arguments | |
19 | from IPython.core.magic import Magics, magics_class, line_magic |
|
19 | from IPython.core.magic import Magics, magics_class, line_magic | |
20 | from IPython.testing.skipdoctest import skip_doctest |
|
20 | from IPython.testing.skipdoctest import skip_doctest | |
21 | from IPython.utils.warn import warn |
|
21 | from IPython.utils.warn import warn | |
22 | from IPython.core.pylabtools import backends |
|
22 | from IPython.core.pylabtools import backends | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Magic implementation classes |
|
25 | # Magic implementation classes | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | magic_gui_arg = magic_arguments.argument( |
|
28 | magic_gui_arg = magic_arguments.argument( | |
29 | 'gui', nargs='?', |
|
29 | 'gui', nargs='?', | |
30 | help="""Name of the matplotlib backend to use %s. |
|
30 | help="""Name of the matplotlib backend to use %s. | |
31 | If given, the corresponding matplotlib backend is used, |
|
31 | If given, the corresponding matplotlib backend is used, | |
32 | otherwise it will be matplotlib's default |
|
32 | otherwise it will be matplotlib's default | |
33 | (which you can set in your matplotlib config file). |
|
33 | (which you can set in your matplotlib config file). | |
34 | """ % str(tuple(sorted(backends.keys()))) |
|
34 | """ % str(tuple(sorted(backends.keys()))) | |
35 | ) |
|
35 | ) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | @magics_class |
|
38 | @magics_class | |
39 | class PylabMagics(Magics): |
|
39 | class PylabMagics(Magics): | |
40 | """Magics related to matplotlib's pylab support""" |
|
40 | """Magics related to matplotlib's pylab support""" | |
41 |
|
41 | |||
42 | @skip_doctest |
|
42 | @skip_doctest | |
43 | @line_magic |
|
43 | @line_magic | |
44 | @magic_arguments.magic_arguments() |
|
44 | @magic_arguments.magic_arguments() | |
45 |
@magic_arguments.argument('-l', '--list', default= |
|
45 | @magic_arguments.argument('-l', '--list', default=False, action='store_true', | |
46 | help='Show available matplotlib backends') |
|
46 | help='Show available matplotlib backends') | |
47 | @magic_gui_arg |
|
47 | @magic_gui_arg | |
48 | def matplotlib(self, line=''): |
|
48 | def matplotlib(self, line=''): | |
49 | """Set up matplotlib to work interactively. |
|
49 | """Set up matplotlib to work interactively. | |
50 |
|
50 | |||
51 | This function lets you activate matplotlib interactive support |
|
51 | This function lets you activate matplotlib interactive support | |
52 | at any point during an IPython session. It does not import anything |
|
52 | at any point during an IPython session. It does not import anything | |
53 | into the interactive namespace. |
|
53 | into the interactive namespace. | |
54 |
|
54 | |||
55 | If you are using the inline matplotlib backend in the IPython Notebook |
|
55 | If you are using the inline matplotlib backend in the IPython Notebook | |
56 | you can set which figure formats are enabled using the following:: |
|
56 | you can set which figure formats are enabled using the following:: | |
57 |
|
57 | |||
58 | In [1]: from IPython.display import set_matplotlib_formats |
|
58 | In [1]: from IPython.display import set_matplotlib_formats | |
59 |
|
59 | |||
60 | In [2]: set_matplotlib_formats('pdf', 'svg') |
|
60 | In [2]: set_matplotlib_formats('pdf', 'svg') | |
61 |
|
61 | |||
62 | The default for inline figures sets `bbox_inches` to 'tight'. This can |
|
62 | The default for inline figures sets `bbox_inches` to 'tight'. This can | |
63 | cause discrepancies between the displayed image and the identical |
|
63 | cause discrepancies between the displayed image and the identical | |
64 | image created using `savefig`. This behavior can be disabled using the |
|
64 | image created using `savefig`. This behavior can be disabled using the | |
65 | `%config` magic:: |
|
65 | `%config` magic:: | |
66 |
|
66 | |||
67 | In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None} |
|
67 | In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None} | |
68 |
|
68 | |||
69 | In addition, see the docstring of |
|
69 | In addition, see the docstring of | |
70 | `IPython.display.set_matplotlib_formats` and |
|
70 | `IPython.display.set_matplotlib_formats` and | |
71 | `IPython.display.set_matplotlib_close` for more information on |
|
71 | `IPython.display.set_matplotlib_close` for more information on | |
72 | changing additional behaviors of the inline backend. |
|
72 | changing additional behaviors of the inline backend. | |
73 |
|
73 | |||
74 | Examples |
|
74 | Examples | |
75 | -------- |
|
75 | -------- | |
76 | To enable the inline backend for usage with the IPython Notebook:: |
|
76 | To enable the inline backend for usage with the IPython Notebook:: | |
77 |
|
77 | |||
78 | In [1]: %matplotlib inline |
|
78 | In [1]: %matplotlib inline | |
79 |
|
79 | |||
80 | In this case, where the matplotlib default is TkAgg:: |
|
80 | In this case, where the matplotlib default is TkAgg:: | |
81 |
|
81 | |||
82 | In [2]: %matplotlib |
|
82 | In [2]: %matplotlib | |
83 | Using matplotlib backend: TkAgg |
|
83 | Using matplotlib backend: TkAgg | |
84 |
|
84 | |||
85 | But you can explicitly request a different GUI backend:: |
|
85 | But you can explicitly request a different GUI backend:: | |
86 |
|
86 | |||
87 | In [3]: %matplotlib qt |
|
87 | In [3]: %matplotlib qt | |
88 | """ |
|
|||
89 |
|
|
88 | ||
90 | args = magic_arguments.parse_argstring(self.matplotlib, line) |
|
89 | You can list the available backends using the -l/--list option | |
91 |
|
|
90 | ||
92 | if args.list is not None: |
|
91 | In [4]: %matplotlib --list | |
|
92 | Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg', | |||
|
93 | 'gtk', 'tk', 'inline'] | |||
|
94 | """ | |||
|
95 | args = magic_arguments.parse_argstring(self.matplotlib, line) | |||
|
96 | if args.list: | |||
93 | backends_list = list(backends.keys()) |
|
97 | backends_list = list(backends.keys()) | |
94 | print("Available matplotlib backends: %s" % backends_list) |
|
98 | print("Available matplotlib backends: %s" % backends_list) | |
95 | else: |
|
99 | else: | |
96 | gui, backend = self.shell.enable_matplotlib(args.gui) |
|
100 | gui, backend = self.shell.enable_matplotlib(args.gui) | |
97 | self._show_matplotlib_backend(args.gui, backend) |
|
101 | self._show_matplotlib_backend(args.gui, backend) | |
98 |
|
102 | |||
99 | @skip_doctest |
|
103 | @skip_doctest | |
100 | @line_magic |
|
104 | @line_magic | |
101 | @magic_arguments.magic_arguments() |
|
105 | @magic_arguments.magic_arguments() | |
102 | @magic_arguments.argument( |
|
106 | @magic_arguments.argument( | |
103 | '--no-import-all', action='store_true', default=None, |
|
107 | '--no-import-all', action='store_true', default=None, | |
104 | help="""Prevent IPython from performing ``import *`` into the interactive namespace. |
|
108 | help="""Prevent IPython from performing ``import *`` into the interactive namespace. | |
105 |
|
109 | |||
106 | You can govern the default behavior of this flag with the |
|
110 | You can govern the default behavior of this flag with the | |
107 | InteractiveShellApp.pylab_import_all configurable. |
|
111 | InteractiveShellApp.pylab_import_all configurable. | |
108 | """ |
|
112 | """ | |
109 | ) |
|
113 | ) | |
110 | @magic_gui_arg |
|
114 | @magic_gui_arg | |
111 | def pylab(self, line=''): |
|
115 | def pylab(self, line=''): | |
112 | """Load numpy and matplotlib to work interactively. |
|
116 | """Load numpy and matplotlib to work interactively. | |
113 |
|
117 | |||
114 | This function lets you activate pylab (matplotlib, numpy and |
|
118 | This function lets you activate pylab (matplotlib, numpy and | |
115 | interactive support) at any point during an IPython session. |
|
119 | interactive support) at any point during an IPython session. | |
116 |
|
120 | |||
117 | %pylab makes the following imports:: |
|
121 | %pylab makes the following imports:: | |
118 |
|
122 | |||
119 | import numpy |
|
123 | import numpy | |
120 | import matplotlib |
|
124 | import matplotlib | |
121 | from matplotlib import pylab, mlab, pyplot |
|
125 | from matplotlib import pylab, mlab, pyplot | |
122 | np = numpy |
|
126 | np = numpy | |
123 | plt = pyplot |
|
127 | plt = pyplot | |
124 |
|
128 | |||
125 | from IPython.display import display |
|
129 | from IPython.display import display | |
126 | from IPython.core.pylabtools import figsize, getfigs |
|
130 | from IPython.core.pylabtools import figsize, getfigs | |
127 |
|
131 | |||
128 | from pylab import * |
|
132 | from pylab import * | |
129 | from numpy import * |
|
133 | from numpy import * | |
130 |
|
134 | |||
131 | If you pass `--no-import-all`, the last two `*` imports will be excluded. |
|
135 | If you pass `--no-import-all`, the last two `*` imports will be excluded. | |
132 |
|
136 | |||
133 | See the %matplotlib magic for more details about activating matplotlib |
|
137 | See the %matplotlib magic for more details about activating matplotlib | |
134 | without affecting the interactive namespace. |
|
138 | without affecting the interactive namespace. | |
135 | """ |
|
139 | """ | |
136 | args = magic_arguments.parse_argstring(self.pylab, line) |
|
140 | args = magic_arguments.parse_argstring(self.pylab, line) | |
137 | if args.no_import_all is None: |
|
141 | if args.no_import_all is None: | |
138 | # get default from Application |
|
142 | # get default from Application | |
139 | if Application.initialized(): |
|
143 | if Application.initialized(): | |
140 | app = Application.instance() |
|
144 | app = Application.instance() | |
141 | try: |
|
145 | try: | |
142 | import_all = app.pylab_import_all |
|
146 | import_all = app.pylab_import_all | |
143 | except AttributeError: |
|
147 | except AttributeError: | |
144 | import_all = True |
|
148 | import_all = True | |
145 | else: |
|
149 | else: | |
146 | # nothing specified, no app - default True |
|
150 | # nothing specified, no app - default True | |
147 | import_all = True |
|
151 | import_all = True | |
148 | else: |
|
152 | else: | |
149 | # invert no-import flag |
|
153 | # invert no-import flag | |
150 | import_all = not args.no_import_all |
|
154 | import_all = not args.no_import_all | |
151 |
|
155 | |||
152 | gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all) |
|
156 | gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all) | |
153 | self._show_matplotlib_backend(args.gui, backend) |
|
157 | self._show_matplotlib_backend(args.gui, backend) | |
154 | print ("Populating the interactive namespace from numpy and matplotlib") |
|
158 | print ("Populating the interactive namespace from numpy and matplotlib") | |
155 | if clobbered: |
|
159 | if clobbered: | |
156 | warn("pylab import has clobbered these variables: %s" % clobbered + |
|
160 | warn("pylab import has clobbered these variables: %s" % clobbered + | |
157 | "\n`%matplotlib` prevents importing * from pylab and numpy" |
|
161 | "\n`%matplotlib` prevents importing * from pylab and numpy" | |
158 | ) |
|
162 | ) | |
159 |
|
163 | |||
160 | def _show_matplotlib_backend(self, gui, backend): |
|
164 | def _show_matplotlib_backend(self, gui, backend): | |
161 | """show matplotlib message backend message""" |
|
165 | """show matplotlib message backend message""" | |
162 | if not gui or gui == 'auto': |
|
166 | if not gui or gui == 'auto': | |
163 | print("Using matplotlib backend: %s" % backend) |
|
167 | print("Using matplotlib backend: %s" % backend) |
General Comments 0
You need to be logged in to leave comments.
Login now