Python numpy.meshgrid

关于该方法的解释,官网有详细的介绍。

numpy.meshgrid(*xi, **kwargs)

从两个或多个坐标向量返回坐标矩阵。
给定一维坐标阵列x1,x2,…,xn,用N-D坐标数组对矢量化的N-D标量/矢量场在N-D网格上的评估。

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:
此函数通过索引关键字参数支持两种索引约定。 给字符串’ij’返回一个带有矩阵索引的meshgrid,’xy’返回一个带有笛卡尔索引的meshgrid。在具有长度为M和N的输入的二维情况下,对于’xy’索引,输出具有形状(N,M),对于’ij’索引,输出具有(M,N)。在输入长度为M,N和P的三维情况下,对于’xy’索引,输出具有形状(N,M,P),对于’ij’索引,输出具有(M,N,P)。 以下代码片段说明了不同之处:

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]

在1-D和0-D情况下,索引和稀疏关键字不起作用。

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]]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值