86

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!

j08lue
  • 1,647
  • 2
  • 21
  • 37
scls
  • 16,591
  • 10
  • 44
  • 55

2 Answers2

91

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
  • Thanks for your reply. But I don't understand what I should use if I have 3 parameters (or more) let's called them x1, x2, x3 ! – scls Sep 13 '12 at 18:48
  • 2
    Meshgrid is explicitly 2D. The others all support more dimensions. That would actually explain the existence of ndgrid. – chthonicdaemon Sep 14 '12 at 03:55
  • 3
    The results of meshgrid and mgrid are different. Just try mgrid[1:4, 1:4] and meshgrid([1,2,3], [1,2,3]). – FJDU Jul 30 '13 at 18:30
  • 1
    In the second section where you do `XX = XX.T` it should really be `YY = YY.T`. This becomes obvious if x and y are different. – MountainX Jan 06 '14 at 17:51
  • why would you like to avoid using linspace? – Manuel Pena Sep 10 '19 at 14:08
  • @ManuelPena It's not that there's something wrong with `linspace`, just that `mgrid` does the same thing as the combination of `linspace` and `meshgrid` more directly. – chthonicdaemon Sep 10 '19 at 15:47
  • scitools (not to be confused with scikit-learn) is a Python library that has not fully been ported to Python 3. In matlab, `ndgrid` is a multi-dimensional version of `meshgrid`, but given the current status of scitools, I would say Python doesn't have an `ndgrid` function at all. – Josiah Yoder Jun 19 '20 at 18:22
  • `numpy`'s `mgrid` and `ogrid` behave like Matlab's `ndgrid`: They support multiple dimensions and they use the same axis ordering as a numpy array. In the examples above, the `YY` and `XX` are reversed for `mgrid` and `ogrid` because `YY` varies along the first dimension of the numpy array. – Josiah Yoder Jun 19 '20 at 18:38
  • Does using broadcasting actually save me some memory? – amzon-ex Nov 12 '21 at 17:38
  • @amzon-ex yes, since you’re not building the whole matrices. – chthonicdaemon Nov 13 '21 at 18:07
15

np.mgrid and np.meshgrid() do the same thing but the first and the second axis are swapped:

# 3D
d1, d2, d3 = np.mgrid[0:10, 0:10, 0:10]
d11, d22, d33 = np.meshgrid(np.arange(10),np.arange(10),np.arange(10))
np.array_equal(d1,d11)

yields False. Just swap the first two dimensions:

d11 = np.transpose(d11,[1,0,2])
np.array_equal(d1,d11)

yields True.

This dimension swapping needs to be done for all three arrays d11, d22 and d33.

dopexxx
  • 2,298
  • 19
  • 30
  • 1
    This answer was gold! I was utterly suck trying to recreate specific results (plotting mgrid with colors) until I out of curiosity looked up the difference between meshgrid and mgrid – Rafay Khan Dec 14 '22 at 17:18