# Python numpy.meshgrid

numpy.meshgrid(*xi, **kwargs)

Parameters :
x1, x2,…, xn : array_like
1-D arrays representing the coordinates of a grid.
indexing : {‘xy’, ‘ij’}, optional
Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.
sparse : bool, optional
If True a sparse grid is returned in order to conserve memory. Default is False.
copy : bool, optional
If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note that sparse=False, copy=False will likely return non-contiguous arrays. Furthermore, more than one element of a broadcast array may refer to a single memory location. If you need to write to the arrays, make copies first.
Returns :
X1, X2,…, XN : ndarray
For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) , return (N1, N2, N3,…Nn) shaped arrays if indexing=’ij’ or (N2, N1, N3,…Nn) shaped arrays if indexing=’xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

Notes:

xv, yv = meshgrid(x, y, sparse=False, indexing='ij')
for i in range(nx):
for j in range(ny):
# treat xv[i,j], yv[i,j]

xv, yv = meshgrid(x, y, sparse=False, indexing='xy')
for i in range(nx):
for j in range(ny):
# treat xv[j,i], yv[j,i]

Examples:
[X,Y]= meshgrid(x,y);
这里meshigrid（x，y）的作用是产生一个以向量x为行，向量y为列的矩阵，而x是从-3开始到3，每间隔1记下一个数据，并把这些数据集成矩阵X；同理y则是从-2到2，每间隔1记下一个数据，并集成矩阵Y。即

>>> import numpy as np
>>> x = np.linspace(0, 1, nx)
>>>
>>> x
array([ 0. ,  0.5,  1. ])
>>> y = np.linspace(0, 1, ny)
>>> y
array([ 0.,  1.])
>>> xv, yv = np.meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
[ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
[ 1.,  1.,  1.]])

In [7]: x = np.arange(3)

In [8]: y = np.arange(4)

In [9]: x
Out[9]: array([0, 1, 2])

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

In [13]: m,n = np.meshgrid(x,y)

In [14]: m
Out[14]:
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])

In [15]: n
Out[15]:
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
In [16]: x = np.arange(2)

In [17]: y = np.arange(3)

In [18]: z = np.arange(4)

In [19]: m,n,l = np.meshgrid(x,y,z)

In [20]: x
Out[20]: array([0, 1])

In [21]: y
Out[21]: array([0, 1, 2])

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

In [23]: m
Out[23]:
array([[[0, 0, 0, 0],
[1, 1, 1, 1]],

[[0, 0, 0, 0],
[1, 1, 1, 1]],

[[0, 0, 0, 0],
[1, 1, 1, 1]]])

In [24]: n
Out[24]:
array([[[0, 0, 0, 0],
[0, 0, 0, 0]],

[[1, 1, 1, 1],
[1, 1, 1, 1]],

[[2, 2, 2, 2],
[2, 2, 2, 2]]])

In [25]: l
Out[25]:
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]]])


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120