Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mesh grid functions in Python (meshgrid mgrid ogrid ndgrid)

I'm looking for a clear comparison of meshgrid-like functions. Unfortunately I don't find it!

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

  • mgrid

  • ogrid

  • meshgrid

Scitools http://hplgit.github.io/scitools/doc/api/html/index.html provides

  • ndgrid

  • boxgrid

Ideally a table summarizing all this would be perfect!

like image 774
scls Avatar asked Sep 13 '12 08:09

scls


People also ask

What is Meshgrid function in Python?

meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing. Meshgrid function is somewhat inspired from MATLAB. Consider the above figure with X-axis ranging from -4 to 4 and Y-axis ranging from -5 to 5.

What is Mgrid in Python?

The mgrid() function is used to get a dense multi-dimensional 'meshgrid'. An instance of numpy. lib. index_tricks. nd_grid which returns an dense (or fleshed out) mesh-grid when indexed, so that each returned argument has the same shape.

What is NumPy Ogrid?

NumPy: ogrid() function index_tricks. nd_grid which returns an open (i.e. not fleshed out) mesh-grid when indexed, so that only one dimension of each returned array is greater than 1. The dimension and number of the output arrays are equal to the number of indexing dimensions.

What does Meshgrid return?

Description. [ X , Y ] = meshgrid( x , y ) returns 2-D grid coordinates based on the coordinates contained in vectors x and y . X is a matrix where each row is a copy of x , and Y is a matrix where each column is a copy of y .


1 Answers

numpy.meshgrid is modelled after Matlab's meshgrid command. It is used to vectorise functions of two variables, so that 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 the combinations of x and y put into the function. When you think about it, meshgrid is a bit superfluous for numpy arrays, as they broadcast. This 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 which use index notation so that you can create XX and YY in the previous examples directly, without having to use something like linspace. The order in which the output are 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 the scitools stuff, but ndgrid seems equivalent to meshgrid, while BoxGrid is actually a whole class to help with this kind of generation.

like image 188
chthonicdaemon Avatar answered Oct 04 '22 18:10

chthonicdaemon