[Numpy-discussion] Meshgrid and mgrid Differences

Jed Ludlow jed.ludlow@gmail....
Thu Jul 15 12:04:43 CDT 2010


Hello, all.

Is there a technical reason that 'meshgrid' and 'mgrid' produce results
which differ from each other by a transpose? For example,

In [1]: X,Y = meshgrid(array([0,1,2,3]), array([0,1,2,3,4,5]))

In [2]: X
Out[2]:
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

In [3]: Y
Out[3]:
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5]])

In [4]: U,V = mgrid[0:4,0:6]

In [5]: U
Out[5]:
array([[0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3]])

In [6]: V
Out[6]:
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

The functions seemed to be designed to do a similar task. It may not seem
like a major difference, but it does make for interesting downstream
effects, particularly when working with mayavi.mlab. When you call
mlab.surf(s) it gets the axes sense correct only if you used mgrid to
generate the x-y grid for evaluating s. If you used meshgrid to generate the
x-y grid the surface gets rotated in the scene by 90 degrees.

Regards,

Jed Ludlow
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100715/3a983a00/attachment.html 


More information about the NumPy-Discussion mailing list