首先先上文档解释
https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html
文档:
meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.
Changed in version 1.9: 1-D and 0-D cases are allowed.
从坐标向量中返回坐标矩阵
在给定一维数组[x1,x2...xn]的情况下,返回N - D维的的坐标矩阵
在新版本中可以返回1-D或0-D的坐标矩阵
官方示例
>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = np.meshgrid(x, y)
>>> xv
array([[0. , 0.5, 1. ],
[0. , 0.5, 1. ]])
>>> yv
array([[0., 0., 0.],
[1., 1., 1.]])
>>> xv, yv = np.meshgrid(x, y, sparse=True) # make sparse output arrays
>>> xv
array([[0. , 0.5, 1. ]])
>>> yv
array([[0.],
[1.]])
个人的理解是x的元素数是meshgrid后的横坐标元素数
y的元素数是meshgrid后的纵坐标元素数
sparse=True这个参数应该是返回数组,并把yv变成转置形式
示例二
nx,ny = (4,4) #从0开始到1结束,返回一个numpy数组,nx代表数组中元素的个数
x = np.linspace(0,3,nx) # [0. 1. 2. 3]
y = np.linspace(0,9,ny) # [0. 3. 6. 9]
xv,yv = np.meshgrid(x,y)
print(xv.ravel()) #[0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3.]
print(yv.ravel()) #[0. 0. 0. 0. 3. 3. 3. 3. 6. 6. 6. 6. 9. 9. 9. 9.]