numpy.meshgrid() in Python
numpy.meshgrid() in Python
The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors.
Syntax
numpy.meshgrid(*xi, **kwargs)
Parameter
The numpy.meshgrid() function consists of four parameters which are as follow:
x1, x2,…, xn: This parameter signifies 1-D arrays representing the coordinates of a grid.
indexing : {‘xy’, ‘ij’}, optional It is an optional parameter representing the cartesian (‘xy’, default) or matrix indexing of output.
sparse: It is an optional parameter which takes Boolean value. If ‘True’ is passed then a sparse grid is returned in order to conserve memory.
copy: It is an optional parameter which takes Boolean value. If ‘False’ is passed, a view into the original arrays are returned to conserve the memory.
Return
This function returns the coordinate length from coordinate vectors.
Example 1
# Program to Explain # numpy.meshgrid() fucntion import numpy as np x = np.linspace(-2, 2, 9) # numpy.linspace creates an array of # 9 linearly placed elements between y = np.linspace(-5, 5, 11) # meshgrid two 2-dimensional arrays x_1, y_1 = np.meshgrid(x, y) print("x_1 Matrix: ") print(x_1) print("y_1 Matrix: ") print(y_1)
Output
x_1 Matrix: [[-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ] [-2. -1.5 -1. -0.5 0. 0.5 1. 1.5 2. ]] y_1 Matrix: [[-5. -5. -5. -5. -5. -5. -5. -5. -5.] [-4. -4. -4. -4. -4. -4. -4. -4. -4.] [-3. -3. -3. -3. -3. -3. -3. -3. -3.] [-2. -2. -2. -2. -2. -2. -2. -2. -2.] [-1. -1. -1. -1. -1. -1. -1. -1. -1.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4. 4. 4. 4. 4.] [ 5. 5. 5. 5. 5. 5. 5. 5. 5.]]
Example 2
# Program to explain the Matrix indexing import numpy as np x = np.linspace(-3, 3, 9) y = np.linspace(-4, 4, 11) # The meshgrid function returns # two 2-dimensional arrays x_1, y_1 = np.meshgrid(x, y) x_2, y_2 = np.meshgrid(x, y, indexing = 'ij') # The below code checks if matrix x_2 and matrix y_2 are the # transposes of matrix x_1 and matrix y_1 print("x_2 Matrix: ") print(x_2) print("y_2 Matrix: ") print(y_2)
Output
x_2 Matrix: [[-3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. ] [-2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25] [-1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 ] [-0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75] [ 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 ] [ 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25] [ 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. ]] y_2 Matrix: [[-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ] [-4. -3.2 -2.4 -1.6 -0.8 0. 0.8 1.6 2.4 3.2 4. ]]