Show More
@@ -83,7 +83,26 b' def compress_dhist(dh):' | |||
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | def pylab_activate(user_ns, gui=None, import_all=True): |
|
86 | """....""" | |
|
86 | """Activate pylab mode in the user's namespace. | |
|
87 | ||
|
88 | Loads and initializes numpy, matplotlib and friends for interactive use. | |
|
89 | ||
|
90 | Parameters | |
|
91 | ---------- | |
|
92 | user_ns : dict | |
|
93 | Namespace where the imports will occur. | |
|
94 | ||
|
95 | gui : optional, string | |
|
96 | A valid gui name following the conventions of the %gui magic. | |
|
97 | ||
|
98 | import_all : optional, boolean | |
|
99 | If true, an 'import *' is done from numpy and pylab. | |
|
100 | ||
|
101 | Returns | |
|
102 | ------- | |
|
103 | The actual gui used (if not given as input, it was obtained from matplotlib | |
|
104 | itself, and will be needed next to configure IPython's gui integration. | |
|
105 | """ | |
|
87 | 106 | |
|
88 | 107 | # Initialize matplotlib to interactive mode always |
|
89 | 108 | import matplotlib |
@@ -106,12 +125,18 b' def pylab_activate(user_ns, gui=None, import_all=True):' | |||
|
106 | 125 | b2g = dict(zip(g2b.values(),g2b.keys())) |
|
107 | 126 | gui = b2g[backend] |
|
108 | 127 | |
|
128 | # We must set the desired backend before importing pylab | |
|
109 | 129 | matplotlib.use(backend) |
|
110 | 130 | |
|
111 | 131 | # This must be imported last in the matplotlib series, after |
|
112 | 132 | # backend/interactivity choices have been made |
|
113 | 133 | import matplotlib.pylab as pylab |
|
114 | 134 | |
|
135 | # XXX For now leave this commented out, but depending on discussions with | |
|
136 | # mpl-dev, we may be able to allow interactive switching... | |
|
137 | #import matplotlib.pyplot | |
|
138 | #matplotlib.pyplot.switch_backend(backend) | |
|
139 | ||
|
115 | 140 | pylab.show._needmain = False |
|
116 | 141 | # We need to detect at runtime whether show() is called by the user. |
|
117 | 142 | # For this, we wrap it into a decorator which adds a 'called' flag. |
@@ -132,35 +157,53 b' def pylab_activate(user_ns, gui=None, import_all=True):' | |||
|
132 | 157 | |
|
133 | 158 | matplotlib.interactive(True) |
|
134 | 159 | |
|
135 | # matplotlib info banner | |
|
136 | 160 | print """ |
|
137 | 161 | Welcome to pylab, a matplotlib-based Python environment. |
|
138 | 162 | Backend in use: %s |
|
139 |
For more information, type 'help(pylab)'. |
|
|
163 | For more information, type 'help(pylab)'.""" % backend | |
|
164 | ||
|
140 | 165 | return gui |
|
141 | 166 | |
|
167 | # We need a little factory function here to create the closure where | |
|
168 | # safe_execfile can live. | |
|
142 | 169 | def mpl_runner(safe_execfile): |
|
143 | def mplot_exec(fname,*where,**kw): | |
|
144 | """Execute a matplotlib script. | |
|
170 | """Factory to return a matplotlib-enabled runner for %run. | |
|
171 | ||
|
172 | Parameters | |
|
173 | ---------- | |
|
174 | safe_execfile : function | |
|
175 | This must be a function with the same interface as the | |
|
176 | :meth:`safe_execfile` method of IPython. | |
|
177 | ||
|
178 | Returns | |
|
179 | ------- | |
|
180 | A function suitable for use as the ``runner`` argument of the %run magic | |
|
181 | function. | |
|
182 | """ | |
|
183 | ||
|
184 | def mpl_execfile(fname,*where,**kw): | |
|
185 | """matplotlib-aware wrapper around safe_execfile. | |
|
145 | 186 | |
|
146 | This is a call to execfile(), but wrapped in safeties to properly | |
|
147 | handle interactive rendering and backend switching.""" | |
|
187 | Its interface is identical to that of the :func:`execfile` builtin. | |
|
188 | ||
|
189 | This is ultimately a call to execfile(), but wrapped in safeties to | |
|
190 | properly handle interactive rendering.""" | |
|
148 | 191 | |
|
149 | 192 | import matplotlib |
|
150 | 193 | import matplotlib.pylab as pylab |
|
151 | 194 | |
|
152 | 195 | #print '*** Matplotlib runner ***' # dbg |
|
153 | 196 | # turn off rendering until end of script |
|
154 |
is |
|
|
197 | is_interactive = matplotlib.rcParams['interactive'] | |
|
155 | 198 | matplotlib.interactive(False) |
|
156 | 199 | safe_execfile(fname,*where,**kw) |
|
157 |
matplotlib.interactive(is |
|
|
200 | matplotlib.interactive(is_interactive) | |
|
158 | 201 | # make rendering call now, if the user tried to do it |
|
159 | 202 | if pylab.draw_if_interactive.called: |
|
160 | 203 | pylab.draw() |
|
161 | 204 | pylab.draw_if_interactive.called = False |
|
162 | 205 | |
|
163 |
return mpl |
|
|
206 | return mpl_execfile | |
|
164 | 207 | |
|
165 | 208 | |
|
166 | 209 | #*************************************************************************** |
@@ -3562,7 +3605,7 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
3562 | 3605 | using the (pylab/wthread/etc.) command line flags. GUI toolkits |
|
3563 | 3606 | can now be enabled, disabled and swtiched at runtime and keyboard |
|
3564 | 3607 | interrupts should work without any problems. The following toolkits |
|
3565 |
are support |
|
|
3608 | are supported: wxPython, PyQt4, PyGTK, and Tk:: | |
|
3566 | 3609 | |
|
3567 | 3610 | %gui wx # enable wxPython event loop integration |
|
3568 | 3611 | %gui qt4|qt # enable PyQt4 event loop integration |
@@ -3678,8 +3721,42 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
3678 | 3721 | |
|
3679 | 3722 | _pylab_magic_run.__doc__ = magic_run.__doc__ |
|
3680 | 3723 | |
|
3724 | @testdec.skip_doctest | |
|
3681 | 3725 | def magic_pylab(self, s): |
|
3682 | """Load pylab, optionally with gui of choice""" | |
|
3726 | """Load numpy and matplotlib to work interactively. | |
|
3727 | ||
|
3728 | %pylab [GUINAME] | |
|
3729 | ||
|
3730 | This function lets you activate pylab (matplotlib, numpy and | |
|
3731 | interactive support) at any point during an IPython session. | |
|
3732 | ||
|
3733 | It will import at the top level numpy as np, pyplot as plt, matplotlib, | |
|
3734 | pylab and mlab, as well as all names from numpy and pylab. | |
|
3735 | ||
|
3736 | Parameters | |
|
3737 | ---------- | |
|
3738 | guiname : optional | |
|
3739 | One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or | |
|
3740 | 'tk'). If given, the corresponding Matplotlib backend is used, | |
|
3741 | otherwise matplotlib's default (which you can override in your | |
|
3742 | matplotlib config file) is used. | |
|
3743 | ||
|
3744 | Examples | |
|
3745 | -------- | |
|
3746 | In this case, where the MPL default is TkAgg: | |
|
3747 | In [2]: %pylab | |
|
3748 | ||
|
3749 | Welcome to pylab, a matplotlib-based Python environment. | |
|
3750 | Backend in use: TkAgg | |
|
3751 | For more information, type 'help(pylab)'. | |
|
3752 | ||
|
3753 | But you can explicitly request a different backend: | |
|
3754 | In [3]: %pylab qt | |
|
3755 | ||
|
3756 | Welcome to pylab, a matplotlib-based Python environment. | |
|
3757 | Backend in use: Qt4Agg | |
|
3758 | For more information, type 'help(pylab)'. | |
|
3759 | """ | |
|
3683 | 3760 | |
|
3684 | 3761 | gui = pylab_activate(self.shell.user_ns, s) |
|
3685 | 3762 | self.shell.magic_gui('-a %s' % gui) |
General Comments 0
You need to be logged in to leave comments.
Login now