def _matplotlib_config(self,name,user_ns,user_global_ns=None): """Return items needed to setup the user's shell with matplotlib""" # Initialize matplotlib to interactive mode always import matplotlib from matplotlib import backends matplotlib.interactive(True) def use(arg): """IPython wrapper for matplotlib's backend switcher. In interactive use, we can not allow switching to a different interactive backend, since thread conflicts will most likely crash the python interpreter. This routine does a safety check first, and refuses to perform a dangerous switch. It still allows switching to non-interactive backends.""" if arg in backends.interactive_bk and arg != self.mpl_backend: m=('invalid matplotlib backend switch.\n' 'This script attempted to switch to the interactive ' 'backend: `%s`\n' 'Your current choice of interactive backend is: `%s`\n\n' 'Switching interactive matplotlib backends at runtime\n' 'would crash the python interpreter, ' 'and IPython has blocked it.\n\n' 'You need to either change your choice of matplotlib backend\n' 'by editing your .matplotlibrc file, or run this script as a \n' 'standalone file from the command line, not using IPython.\n' % (arg,self.mpl_backend) ) raise RuntimeError, m else: self.mpl_use(arg) self.mpl_use._called = True self.matplotlib = matplotlib self.mpl_backend = matplotlib.rcParams['backend'] # we also need to block switching of interactive backends by use() self.mpl_use = matplotlib.use self.mpl_use._called = False # overwrite the original matplotlib.use with our wrapper matplotlib.use = use # This must be imported last in the matplotlib series, after # backend/interactivity choices have been made import matplotlib.pylab as pylab self.pylab = pylab self.pylab.show._needmain = False # We need to detect at runtime whether show() is called by the user. # For this, we wrap it into a decorator which adds a 'called' flag. self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) # Build a user namespace initialized with matplotlib/matlab features. user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns, user_global_ns) # Import numpy as np/pyplot as plt are conventions we're trying to # somewhat standardize on. Making them available to users by default # will greatly help this. exec ("import numpy\n" "import numpy as np\n" "import matplotlib\n" "import matplotlib.pylab as pylab\n" "try:\n" " import matplotlib.pyplot as plt\n" "except ImportError:\n" " pass\n" ) in user_ns # Build matplotlib info banner b=""" Welcome to pylab, a matplotlib-based Python environment. For more information, type 'help(pylab)'. """ return user_ns,user_global_ns,b
def _matplotlib_config(self, name): """Return various items needed to setup the user's shell with matplotlib""" # Initialize matplotlib to interactive mode always import matplotlib from matplotlib import backends matplotlib.interactive(True) def use(arg): """IPython wrapper for matplotlib's backend switcher. In interactive use, we can not allow switching to a different interactive backend, since thread conflicts will most likely crash the python interpreter. This routine does a safety check first, and refuses to perform a dangerous switch. It still allows switching to non-interactive backends.""" if arg in backends.interactive_bk and arg != self.mpl_backend: m = ( 'invalid matplotlib backend switch.\n' 'This script attempted to switch to the interactive ' 'backend: `%s`\n' 'Your current choice of interactive backend is: `%s`\n\n' 'Switching interactive matplotlib backends at runtime\n' 'would crash the python interpreter, ' 'and IPython has blocked it.\n\n' 'You need to either change your choice of matplotlib backend\n' 'by editing your .matplotlibrc file, or run this script as a \n' 'standalone file from the command line, not using IPython.\n' % (arg, self.mpl_backend)) raise RuntimeError, m else: self.mpl_use(arg) self.mpl_use._called = True self.matplotlib = matplotlib self.mpl_backend = matplotlib.rcParams['backend'] # we also need to block switching of interactive backends by use() self.mpl_use = matplotlib.use self.mpl_use._called = False # overwrite the original matplotlib.use with our wrapper matplotlib.use = use # This must be imported last in the matplotlib series, after # backend/interactivity choices have been made try: import matplotlib.pylab as pylab self.pylab = pylab self.pylab_name = 'pylab' except ImportError: import matplotlib.matlab as matlab self.pylab = matlab self.pylab_name = 'matlab' self.pylab.show._needmain = False # We need to detect at runtime whether show() is called by the user. # For this, we wrap it into a decorator which adds a 'called' flag. self.pylab.draw_if_interactive = flag_calls( self.pylab.draw_if_interactive) # Build a user namespace initialized with matplotlib/matlab features. user_ns = {'__name__': '__main__', '__builtins__': __builtin__} # Be careful not to remove the final \n in the code string below, or # things will break badly with py22 (I think it's a python bug, 2.3 is # OK). pname = self.pylab_name # Python can't interpolate dotted var names exec( "import matplotlib\n" "import matplotlib.%(pname)s as %(pname)s\n" "from matplotlib.%(pname)s import *\n" % locals()) in user_ns # Build matplotlib info banner b = """ Welcome to pylab, a matplotlib-based Python environment. For more information, type 'help(pylab)'. """ return user_ns, b