##// END OF EJS Templates
include sorted list of backends
Paul Ivanov -
Show More
@@ -1,141 +1,141 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
22
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 # Magic implementation classes
24 # Magic implementation classes
24 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
25
26
26 magic_gui_arg = magic_arguments.argument(
27 magic_gui_arg = magic_arguments.argument(
27 'gui', nargs='?',
28 'gui', nargs='?',
28 help="""Name of the matplotlib backend to use
29 help="""Name of the matplotlib backend to use %s.
29 ('qt', 'wx', 'gtk', 'osx', 'tk', 'inline', 'auto').
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 """
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 The names that will still be added to the namespace if this flag is given::
86
86
87 numpy
87 numpy
88 matplotlib
88 matplotlib
89 np (numpy alias)
89 np (numpy alias)
90 plt (matplotlib.pyplot alias)
90 plt (matplotlib.pyplot alias)
91 pylab (from matplotlib)
91 pylab (from matplotlib)
92 pyplot (from matplotlib)
92 pyplot (from matplotlib)
93 mlab (from matplotlib)
93 mlab (from matplotlib)
94 display (from IPython)
94 display (from IPython)
95 figsize (from IPython)
95 figsize (from IPython)
96 getfigs (from IPython)
96 getfigs (from IPython)
97
97
98 You can govern the default behavior with the
98 You can govern the default behavior with the
99 InteractiveShellApp.pylab_import_all configurable.
99 InteractiveShellApp.pylab_import_all configurable.
100 """
100 """
101 )
101 )
102 @magic_gui_arg
102 @magic_gui_arg
103 def pylab(self, line=''):
103 def pylab(self, line=''):
104 """Load numpy and matplotlib to work interactively.
104 """Load numpy and matplotlib to work interactively.
105
105
106 This function lets you activate pylab (matplotlib, numpy and
106 This function lets you activate pylab (matplotlib, numpy and
107 interactive support) at any point during an IPython session.
107 interactive support) at any point during an IPython session.
108
108
109 It will import at the top level numpy as np, pyplot as plt, matplotlib,
109 It will import at the top level numpy as np, pyplot as plt, matplotlib,
110 pylab and mlab, as well as all names from numpy and pylab.
110 pylab and mlab, as well as all names from numpy and pylab.
111
111
112 See the %matplotlib magic for more details.
112 See the %matplotlib magic for more details.
113 """
113 """
114 args = magic_arguments.parse_argstring(self.pylab, line)
114 args = magic_arguments.parse_argstring(self.pylab, line)
115 if args.no_import_all is None:
115 if args.no_import_all is None:
116 # get default from Application
116 # get default from Application
117 if Application.initialized():
117 if Application.initialized():
118 app = Application.instance()
118 app = Application.instance()
119 try:
119 try:
120 import_all = app.pylab_import_all
120 import_all = app.pylab_import_all
121 except AttributeError:
121 except AttributeError:
122 import_all = True
122 import_all = True
123 else:
123 else:
124 # nothing specified, no app - default True
124 # nothing specified, no app - default True
125 import_all = True
125 import_all = True
126 else:
126 else:
127 # invert no-import flag
127 # invert no-import flag
128 import_all = not args.no_import_all
128 import_all = not args.no_import_all
129
129
130 gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
130 gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
131 self._show_matplotlib_backend(args.gui, backend)
131 self._show_matplotlib_backend(args.gui, backend)
132 if clobbered:
132 if clobbered:
133 warn("pylab import has clobbered these variables: %s" % clobbered +
133 warn("pylab import has clobbered these variables: %s" % clobbered +
134 "\n`%pylab --no-import-all` prevents importing * from pylab and numpy"
134 "\n`%pylab --no-import-all` prevents importing * from pylab and numpy"
135 )
135 )
136
136
137 def _show_matplotlib_backend(self, gui, backend):
137 def _show_matplotlib_backend(self, gui, backend):
138 """show matplotlib message backend message"""
138 """show matplotlib message backend message"""
139 if not gui or gui == 'auto':
139 if not gui or gui == 'auto':
140 print ("using matplotlib backend: %s" % backend)
140 print ("using matplotlib backend: %s" % backend)
141
141
General Comments 0
You need to be logged in to leave comments. Login now