Definition of NumPy Meshgrid
In python, meshgrid is a function that creates a rectangular grid out of 2 given 1-dimensional arrays that denotes the Matrix or Cartesian indexing. It is inspired from MATLAB. This meshgrid function is provided by the module numpy. Coordinate matrices are returned from the coordinate vectors. In this, we will discuss more about meshgrid function in detail.
Syntax
Below is the syntax of meshgrid function in numpy.
numpy.meshgrid(*xi, **kwargs)
Four parameters of this function are:
1. x1, x2,…, xn
- Required parameter
- The parameter that denotes the coordinates of the grids in the 1-D arrays.
2. indexing
- Optional parameter
- The parameter that denotes the cartesian(‘xy’, default) or matrix indexing of result.
3. Sparse
- Optional parameter
- Takes Boolean value
- A sparse grid is returned for conserving memory if ‘True’ is passed.
- Default value: False
4. Copy
- Optional parameter
- Takes Boolean value.
- The original array’s view is returned for conserving memory if ‘false is passed.
- Default value: False
If the parameters sparse and copy are set to False, non-contiguous arrays will be returned. Moreover, 1 or more elements of a broadcast array can point to a single memory location. Copies of the arrays have to be made first if writing has to be done into the arrays. The return value is: Length of the coordinate from the coordinate vectors.
How does Meshgrid Function Work in NumPy?
In order to understand the working of meshgrid function in numpy, let us see an example.
Steps for creating meshgrid:
- Import the module numpy.
importnumpy as np
- Create two variables.
n1, n2 = (5, 3)
- Create two arrays
a = np.linspace(0, 1, n1)
b = np.linspace(0, 1, n2)
- Call the meshgrid function with the arrays as parameters.
aa, bb = np.meshgrid(a, b)
- Display the result
print(aa)
print(bb)
Examples of NumPy Meshgrid
Let us see some sample programs of meshgrid function.
Example #1
Python program to print the meshgrid coordinates of two arrays.
Code:
import numpy as np
n1, n2 = (5, 3)
a = np.linspace(0, 1, n1)
b = np.linspace(0, 1, n2)
aa, bb = np.meshgrid(a, b)
print(aa)
print(bb)
Output:
In this program,numpy modules has to be imported with alias name as np. Then, two variables n1 and n2 are created where the values assigned are 5 and 3. Once this is done, two arrays a and b are also created with the help of linspace() function. Then two variables aa and bb are used for assigning the return value of meshgrid. In the function, both the arrays a and b are passed. Atlast, the value of aa and bb are printed. On executing the code, two arrays will be displayed that consists of coordinate length and coordinate vectors as well. The coordinates given as output by meshgrid can also be used to plot functions that is given in the coordinate range.
Example #2
Python program to print the meshgrid coordinates of two arrays that are between specified values.
Code:
import numpy as np
l = np.linspace(-7, -1, 9)
k = np.linspace(-6, -2, 11)
x1, y1 = np.meshgrid(l, k)
print("x1 is : ")
print(x1)
print("y1 is : ")
print(y1)
Output:
In this program also,numpy modules has to be imported with any alias name. The alias name in this program is np. Then, two arrays l and k are created with the help of linspace() function. Then two variables x1 and y1 are used for assigning the return value of meshgrid. In the function, both the arrays l and k are passed. Atlast, the value of x1 and y1 are printed. On executing the code, two arrays will be displayed that consists of coordinate length and coordinate vectors as well.
Example #3
Python program to print the meshgrid coordinates of two arrays where sparse is true.
Code:
import numpy as np
n1, n2 = (5, 3)
l = np.linspace(0, 1, n1)
k = np.linspace(0, 1, n2)
aa, bb = np.meshgrid(l, k, sparse=True)
print(aa)
print(bb)
Output:
Similar to the above programs, in this program also,numpy modules has to be imported with an alias name. The alias name in this program is np. First, two variables n1 and n2 are created where values are 5 and 3 respectively. Then, two arrays l and k are created with the help of linspace() function. Then two variables x1 and y1 are used for assigning the return value of meshgrid. In addition to the above programs, sparse is true here. If sparse is true, it conserves the memory In the function, both the arrays l, k, and sparse value are passed. At last, the value of x1 and y1 are printed. On executing the code, two arrays will be displayed that consists of coordinate length and coordinate vectors as well. Here, it can be seen that the way of displaying is different from the above programs.
Example #4
Python program to print the meshgrid coordinates of two arrays and display contour lines.
Code:
import numpy as np
import matplotlib.pyplot as plt
n = np.arange( -5, 5, 0.1 )
m = np.arange( -5, 5, 0.1 )
x, y = np.meshgrid( n, m, sparse=True )
c = np.sin( x**2+ y**2 ) / ( x**2 + y**2 )
z = plt.contourf( n, m, c )
plt.show()
Output:
Similar to the above programs, in this program also,numpy modules has to be imported with an alias name np. First, two arrays n and m are created with the help of arange() function. Then two variables x and y are used for assigning the return value of meshgrid Moreover, sparse is true here as well. In the function, both the arrays n, m, and sparse value is passed. Then, a variable z is declared that assigns the return value of the function np.sin(). Atlast, the contour lines along with filled contours are drawn using the function plt.contourf(). On executing the code, it can be seen that the contour lines have been plotted.
Recommended Articles
This is a guide to NumPy Meshgrid. Here we also discuss the working of meshgrid function in numpy along with examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses