The numpy.meshgrid()
function in Python is used to return the coordinate matrices from given coordinate vectors.
numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
The numpy.meshgrid()
function takes the following parameter values:
x1, x2,..., xn
: these represent the 1-D arrays, which also represent the coordinates of a grid.indexing
: this represents the default, Cartesian (xy
), or matrix (ij
) indexing of the output. This is optional.sparse
: this takes a Boolean value. If True
, the shape of the returned coordinate array for dimension i
is reduced from (N1, ..., Ni, ..., Nn)
to (1, ..., 1, Ni, 1, ..., 1)
. This is optional.copy
: this takes a Boolean value. If False
, a view into the original arrays are returned in order to conserve memory. The default value is False
. This is optional.The numpy.meshgrid()
function returns X1, X2,...XN
arrays.
import numpy as np# creating the array_like objectsnx, ny = (2, 3)# implementing the linespace functionx = np.linspace(0, 1, nx)y = np.linspace(0, 1, ny)xv, yv = np.meshgrid(x, y, copy = True, sparse = False, indexing ='xy' )print(xv)print(yv)
Here is a line-by-line explanation of the code above:
numpy
library.nx
and ny
.linspace()
function, we return evenly spaced numbers over a specified interval starting from 0
to 1
for the nx
and ny
array-like objects.meshgrid()
function on the matrices xv
and yv
xv
and yv
.