Method:
np.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
Generates coordinate matrices from coordinate vectors.
Returns:
(np.ndarray1, np.ndarray2 ..., np.ndarrayn)
Parameters:
*xi: array-like-
One or more 1D arrays representing the coordinates.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
a, b = np.meshgrid(x, y)
print(a)
print(b)
'''
Output:
[[1 2 3]
[1 2 3]]
[[4 4 4]
[5 5 5]]
'''
copy: (True or False), Optional-
If True (default), return copies of the arrays. If False, return views. Please refer to np.array(copy=) for a better explnation along with examples.
sparse: (True or False), Optional-
If True, returns a sparse grid (default False).
sparse = False (default) +
A regular array is generated from coordinate vectors.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
a, b = np.meshgrid(x, y, sparse=False)
print(a)
print(b)
'''
Output:
[[1 2 3]
[1 2 3]]
[[4 4 4]
[5 5 5]]
'''
sparse = True (default) +
A sparse matrix is returned from coordinate vectors.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
a, b = np.meshgrid(x, y, sparse=True)
print(a)
print(b)
'''
Output:
[[1 2 3]]
[[4]
[5]]
'''
indexing: ('xy' or 'ij') Optional-
Determines the ordering of the indices.
indexing = 'xy' (default) +
'xy' for matrix indexing.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
a, b = np.meshgrid(x, y, indexing='xy')
print(a)
print(b)
'''
Output:
[[1 2 3]
[1 2 3]]
[[4 4 4]
[5 5 5]]
'''
indexing = 'ij' +
'ij' for Cartesian indexing.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
a, b = np.meshgrid(x, y, indexing='ij')
print(a)
print(b)
'''
Output:
[[1 1]
[2 2]
[3 3]]
[[4 5]
[4 5]
[4 5]]
'''