Python grid mesh functions (meshgrid mgrid ogrid ndgrid)

I am looking for a clear comparison of features like meshgrid. Sorry, I do not find it!

Numpy http://docs.scipy.org/doc/numpy/reference/ provides

  • mgrid

  • ogrid

  • meshgrid

Scitools http://hplgit.imtqy.com/scitools/doc/api/html/index.html provides

  • ndgrid

  • boxgrid

Ideally, a table summarizing all this would be ideal!

+48
python numpy scipy
Sep 13 '12 at 8:12
source share
1 answer

numpy.meshgrid modeled after the Matlab meshgrid . It is used to vectorize the functions of two variables, so you can write

 x = numpy.array([1, 2, 3]) y = numpy.array([10, 20, 30]) XX, YY = numpy.meshgrid(x, y) ZZ = XX + YY ZZ => array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) 

So ZZ contains all combinations of x and y placed in the function. When you think about it, meshgrid little redundant for numpy arrays when they are being translated. That means you can do

 XX, YY = numpy.atleast_2d(x, y) YY = YY.T # transpose to allow broadcasting ZZ = XX + YY 

and get the same result.

mgrid and ogrid are helper classes that use index notation so you can directly create XX and YY in the previous examples without using something like linspace . The order in which the output is generated is reversed.

 YY, XX = numpy.mgrid[10:40:10, 1:4] ZZ = XX + YY # These are equivalent to the output of meshgrid YY, XX = numpy.ogrid[10:40:10, 1:4] ZZ = XX + YY # These are equivalent to the atleast_2d example 

I am not familiar with scitools material, but ndgrid seems to be equivalent to meshgrid , and BoxGrid is really a whole class that will help in this generation.

+61
Sep 13 '12 at 10:40
source share



All Articles