From: Eric Firing <efiring@ha...>  20061230 02:54:14

Belinda, I will give a short answer, and maybe someone else will be able to provide a more complete answer or a reference to one. belinda thom wrote: > Hi, > > I'm using matplotlib w/numerix set to numpy (as described in my prior > post). > > What I am wondering is in what situations one would want to: > > import pylab > import numpy One reason for doing this would be make it clear where your numeric components are really coming from, and to take full advantage of numpy. An idiom that I often use is import pylab as P import numpy as N and then anything that ultimately comes from numpy anyway, like arange, I invoke as N.arange rather than P.arange. Or you can do it all at the top of the script with "from pylab import ..." and "from numpy import ...". > > together, because there is matlabstyle stuff (e.g. matrices, arrays, > cumprod, fft, arange etc.) by importing the pylab package alone. > Pylab is a convenience package that aggregates plotting and numerical functionality in a single namespace while hiding the differences among numeric packages. This support of alternative numeric packages has been an important part of matplotlib, but its utility will diminish and it will be phased out now that numpy is ready to use. An advantage of using numpy directly now, rather than via the parts of it that pylab imports, is that you will be getting accustomed to the new numpy style rather than the compatibility mode. > On a related note, in trying to get a better idea of how math vs. > plot functionality is handled in matplotlib, I looked at mlab.py and > pylab.py; it doesn't seem there is a clear separation of these > functions, but perhaps I'm missing something? > You are correct. Pylab imports everything from matplotlib.numerix.mlab. Confusingly, there is a matplotlib.mlab and a matplotlib.numerix.mlab which uses an mlab from the selected numerical package. The numpy version of mlab, in turn, imports lots of stuff from numpyit is another aggregator. It seems that numpy's mlab is trying to give a Matlablike environment without the plotting, and pylab is trying to do the same thing but with the plotting included. The net results of the attempts of numpy and pylab to make things convenient by aggregating functionality in a single namespace are (1) it really can be convenienta onestop shop, and (2) it can be very confusing with all the possible places things can get imported from, some of them with similar names. Ipython is enormously helpful in showing where things really come from. For example in an "ipython pylab" session, where "from pylab import *" has been done automatically by ipython, we get: In [1]:arange? Type: function Base Class: <type 'function'> String Form: <function arange at 0xb54f8ca4> Namespace: Interactive File: /usr/local/lib/python2.4/sitepackages/numpy/oldnumeric/functions.py Definition: arange(start, stop=None, step=1, typecode=None, dtype=None) Docstring: <no docstring> Note that pylab is pulling in numpy's backwardscompatibility version of arange rather than its native version. Here is another example of different versions of a function with the same name, first the pylab version, then the numpy version: In [3]:linspace? Type: function Base Class: <type 'function'> String Form: <function linspace at 0xb53f59cc> Namespace: Interactive File: /usr/local/lib/python2.4/sitepackages/matplotlib/mlab.py Definition: linspace(xmin, xmax, N) Docstring: <no docstring> In [4]:import numpy In [5]:numpy.linspace? Type: function Base Class: <type 'function'> String Form: <function linspace at 0xb5570df4> Namespace: Interactive File: /usr/local/lib/python2.4/sitepackages/numpy/lib/function_base.py Definition: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False) Docstring: Return evenly spaced numbers. Return num evenly spaced samples from start to stop. If endpoint is True, the last sample is stop. If retstep is True then return the step value used. Depending on what you are doing and on the style you prefer, you may want to use as much as possible from pylab, or you may want to use only a few things from the pylab namespace and then explicitly use the matplotlib and numpy namespaces (and object methods) for everything else. A bit of this is discussed in the matplotlib examples/pythonic_matplotlib.py. I am inclined to directly invoke numpy instead of going through pylab and numerix. That was longer than I intended for this message... but the complexity of A importing things from B and B from A via B.C, etc. is making my head spin. I hope that in the process of switching matplotlib to numpyonly we will be able to simplify all this. Eric 