kilozulu
kilozulu

Reputation: 347

Why does meshgrid order output dimensions differently than the order provided with input?

I'm trying to understand how meshgrid indexes dimensions of the outputs with respect to the inputs. I've noticed it will always transpose the first two dimensions. For example, when providing x with length 7 and y with length 6 it will return the output arrays as a 6x7 array instead of a 7x6 one.

This is clearly by design so I'm trying to understand the why/logic so I can use it as intended. Can someone shine a light on this? Thanks!

import numpy as np

#specify input dimensions of different lengths to aid in identifying which output index a dimension belongs to
x = np.linspace(0,60,7)
y = np.linspace(0,50,6)
z = np.linspace(0,40,5)
i = np.linspace(0,30,4)
j = np.linspace(0,20,3)

#2D mesh grid, output dimensions transposed from input
xx, yy = np.meshgrid(x,y)
print(xx.shape)
print(yy.shape)

#3D mesh grid, first two output dimensions transposed from input
xx, yy, zz = np.meshgrid(x,y,z)
print(xx.shape)
print(yy.shape)
print(zz.shape)

#4D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii = np.meshgrid(x,y,z,i)
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)

#5D meshgrid, first two output dimensions transposed from input, rest are in input order
xx, yy, zz, ii, jj = np.meshgrid(x,y,z,i,j)
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)
print(jj.shape)

Upvotes: 5

Views: 3834

Answers (2)

kilozulu
kilozulu

Reputation: 347

It's possible to do this with the 'indexing' option set to 'ij. This feature was added in numpy version 1.7.0. https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html

import numpy as np

#specify input dimensions of different lengths to aid in identifying which index dimension belongs to in output
x = np.linspace(0,60,7)
y = np.linspace(0,50,6)
z = np.linspace(0,40,5)
i = np.linspace(0,30,4)
j = np.linspace(0,20,3)

#2D mesh grid, output dimensions in input order
xx, yy = np.meshgrid(x,y,indexing='ij')
print(xx.shape)
print(yy.shape)

#3D mesh grid, all output dimensions in input order
xx, yy, zz = np.meshgrid(x,y,z,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)

#4D meshgrid, all output dimensions in input order
xx, yy, zz, ii = np.meshgrid(x,y,z,i,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)

#5D meshgrid, all output dimensions in input order
xx, yy, zz, ii, jj = np.meshgrid(x,y,z,i,j,indexing='ij')
print(xx.shape)
print(yy.shape)
print(zz.shape)
print(ii.shape)
print(jj.shape)

Upvotes: 8

Patol75
Patol75

Reputation: 4547

I think this is the difference between axis coordinates and matrix coordinates. When you think of a set of axes in a reference frame, it makes sense to first mention x and only then y. For example, we have all been taught y = f(x). However, in terms of matrices, we know we need to index by rows first: (0, 1) is the first element of the second column. Now if you try to represent a matrix on a graph, there is a problem, because the x axis will correspond to the columns and y axis to the rows: there is a mismatch. As a result, if you were to obtain a (7, 6) array and simply paste it on the graph, then the coordinates would not match because columns values would be y coordinates. This is, I believe, why a transpose operation is needed, to be consistent between the matrix representation and the axes coordinates.

Upvotes: 5

Related Questions