##// END OF EJS Templates
less %pylab, more cowbell!
Paul Ivanov -
Show More
@@ -1,147 +1,147 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 IPython.config.application import Application
17 from IPython.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_gui_arg
45 @magic_gui_arg
46 def matplotlib(self, line=''):
46 def matplotlib(self, line=''):
47 """Set up matplotlib to work interactively.
47 """Set up matplotlib to work interactively.
48
48
49 This function lets you activate matplotlib interactive support
49 This function lets you activate matplotlib interactive support
50 at any point during an IPython session. It does not import anything
50 at any point during an IPython session. It does not import anything
51 into the interactive namespace.
51 into the interactive namespace.
52
52
53 If you are using the inline matplotlib backend in the IPython Notebook
53 If you are using the inline matplotlib backend in the IPython Notebook
54 you can set which figure formats are enabled using the following::
54 you can set which figure formats are enabled using the following::
55
55
56 In [1]: from IPython.display import set_matplotlib_formats
56 In [1]: from IPython.display import set_matplotlib_formats
57
57
58 In [2]: set_matplotlib_formats('pdf', 'svg')
58 In [2]: set_matplotlib_formats('pdf', 'svg')
59
59
60 See the docstring of `IPython.display.set_matplotlib_formats` and
60 See the docstring of `IPython.display.set_matplotlib_formats` and
61 `IPython.display.set_matplotlib_close` for more information on
61 `IPython.display.set_matplotlib_close` for more information on
62 changing the behavior of the inline backend.
62 changing the behavior of the inline backend.
63
63
64 Examples
64 Examples
65 --------
65 --------
66 To enable the inline backend for usage with the IPython Notebook::
66 To enable the inline backend for usage with the IPython Notebook::
67
67
68 In [1]: %matplotlib inline
68 In [1]: %matplotlib inline
69
69
70 In this case, where the matplotlib default is TkAgg::
70 In this case, where the matplotlib default is TkAgg::
71
71
72 In [2]: %matplotlib
72 In [2]: %matplotlib
73 Using matplotlib backend: TkAgg
73 Using matplotlib backend: TkAgg
74
74
75 But you can explicitly request a different GUI backend::
75 But you can explicitly request a different GUI backend::
76
76
77 In [3]: %matplotlib qt
77 In [3]: %matplotlib qt
78 """
78 """
79 args = magic_arguments.parse_argstring(self.matplotlib, line)
79 args = magic_arguments.parse_argstring(self.matplotlib, line)
80 gui, backend = self.shell.enable_matplotlib(args.gui)
80 gui, backend = self.shell.enable_matplotlib(args.gui)
81 self._show_matplotlib_backend(args.gui, backend)
81 self._show_matplotlib_backend(args.gui, backend)
82
82
83 @skip_doctest
83 @skip_doctest
84 @line_magic
84 @line_magic
85 @magic_arguments.magic_arguments()
85 @magic_arguments.magic_arguments()
86 @magic_arguments.argument(
86 @magic_arguments.argument(
87 '--no-import-all', action='store_true', default=None,
87 '--no-import-all', action='store_true', default=None,
88 help="""Prevent IPython from performing ``import *`` into the interactive namespace.
88 help="""Prevent IPython from performing ``import *`` into the interactive namespace.
89
89
90 You can govern the default behavior of this flag with the
90 You can govern the default behavior of this flag with the
91 InteractiveShellApp.pylab_import_all configurable.
91 InteractiveShellApp.pylab_import_all configurable.
92 """
92 """
93 )
93 )
94 @magic_gui_arg
94 @magic_gui_arg
95 def pylab(self, line=''):
95 def pylab(self, line=''):
96 """Load numpy and matplotlib to work interactively.
96 """Load numpy and matplotlib to work interactively.
97
97
98 This function lets you activate pylab (matplotlib, numpy and
98 This function lets you activate pylab (matplotlib, numpy and
99 interactive support) at any point during an IPython session.
99 interactive support) at any point during an IPython session.
100
100
101 %pylab makes the following imports::
101 %pylab makes the following imports::
102
102
103 import numpy
103 import numpy
104 import matplotlib
104 import matplotlib
105 from matplotlib import pylab, mlab, pyplot
105 from matplotlib import pylab, mlab, pyplot
106 np = numpy
106 np = numpy
107 plt = pyplot
107 plt = pyplot
108
108
109 from IPython.display import display
109 from IPython.display import display
110 from IPython.core.pylabtools import figsize, getfigs
110 from IPython.core.pylabtools import figsize, getfigs
111
111
112 from pylab import *
112 from pylab import *
113 from numpy import *
113 from numpy import *
114
114
115 If you pass `--no-import-all`, the last two `*` imports will be excluded.
115 If you pass `--no-import-all`, the last two `*` imports will be excluded.
116
116
117 See the %matplotlib magic for more details about activating matplotlib
117 See the %matplotlib magic for more details about activating matplotlib
118 without affecting the interactive namespace.
118 without affecting the interactive namespace.
119 """
119 """
120 args = magic_arguments.parse_argstring(self.pylab, line)
120 args = magic_arguments.parse_argstring(self.pylab, line)
121 if args.no_import_all is None:
121 if args.no_import_all is None:
122 # get default from Application
122 # get default from Application
123 if Application.initialized():
123 if Application.initialized():
124 app = Application.instance()
124 app = Application.instance()
125 try:
125 try:
126 import_all = app.pylab_import_all
126 import_all = app.pylab_import_all
127 except AttributeError:
127 except AttributeError:
128 import_all = True
128 import_all = True
129 else:
129 else:
130 # nothing specified, no app - default True
130 # nothing specified, no app - default True
131 import_all = True
131 import_all = True
132 else:
132 else:
133 # invert no-import flag
133 # invert no-import flag
134 import_all = not args.no_import_all
134 import_all = not args.no_import_all
135
135
136 gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
136 gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
137 self._show_matplotlib_backend(args.gui, backend)
137 self._show_matplotlib_backend(args.gui, backend)
138 print ("Populating the interactive namespace from numpy and matplotlib")
138 print ("Populating the interactive namespace from numpy and matplotlib")
139 if clobbered:
139 if clobbered:
140 warn("pylab import has clobbered these variables: %s" % clobbered +
140 warn("pylab import has clobbered these variables: %s" % clobbered +
141 "\n`%pylab --no-import-all` prevents importing * from pylab and numpy"
141 "\n`%matplotlib` prevents importing * from pylab and numpy"
142 )
142 )
143
143
144 def _show_matplotlib_backend(self, gui, backend):
144 def _show_matplotlib_backend(self, gui, backend):
145 """show matplotlib message backend message"""
145 """show matplotlib message backend message"""
146 if not gui or gui == 'auto':
146 if not gui or gui == 'auto':
147 print("Using matplotlib backend: %s" % backend)
147 print("Using matplotlib backend: %s" % backend)
General Comments 0
You need to be logged in to leave comments. Login now