VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-meshgrid-function/

⇱ Numpy Meshgrid function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Numpy Meshgrid function

Last Updated : 2 May, 2024
The numpy.meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing. Meshgrid function is somewhat inspired from MATLAB. Consider the below figure with X-axis ranging from -4 to 4 and Y-axis ranging from -5 to 5. So there are a total of (9 * 11) = 99 points marked in the figure each with a X-coordinate and a Y-coordinate. For any line parallel to the X-axis, the X-coordinates of the marked points respectively are -4, -3, -2, -1, 0, 1, 2, 3, 4. On the other hand, for any line parallel to the Y-axis, the Y-coordinates of the marked points from bottom to top are -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5. The numpy.meshgrid function returns two 2-Dimensional arrays representing the X and Y coordinates of all the points. 👁 Image
Examples:
Input : x = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
 y = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] 
Output :
x_1 = array([[-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.],
 [-4., -3., -2., -1., 0., 1., 2., 3., 4.]])

y_1 = array([[-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.]])


Input : x = [0, 1, 2, 3, 4, 5]
 y = [2, 3, 4, 5, 6, 7, 8]

Output :
x_1 = array([[0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.],
 [0., 1., 2., 3., 4., 5.]])

y_1 = array([[2., 2., 2., 2., 2., 2.],
 [3., 3., 3., 3., 3., 3.],
 [4., 4., 4., 4., 4., 4.],
 [5., 5., 5., 5., 5., 5.],
 [6., 6., 6., 6., 6., 6.],
 [7., 7., 7., 7., 7., 7.],
 [8., 8., 8., 8., 8., 8.]]
Below is the code:
Output:
x_1 = 
[[-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]
 [-4. -3. -2. -1. 0. 1. 2. 3. 4.]]
y_1 = 
[[-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.]]
The output of coordinates by meshgrid can also be used for plotting functions within the given coordinate range. An Ellipse: Output: 👁 Image
Random Data: Output: 👁 Image
A Sine function: Output: 👁 Image
We observe that x_1 is a row repeated matrix whereas y_1 is a column repeated matrix. One row of x_1 and one column of y_1 is enough to determine the positions of all the points as the other values will get repeated over and over. So we can edit above code as follows: x_1, y_1 = np.meshgrid(x, y, sparse = True) This will produce the following output:
x_1 = [[-4. -3. -2. -1. 0. 1. 2. 3. 4.]]
y_1 = [[-5.]
 [-4.]
 [-3.]
 [-2.]
 [-1.]
 [ 0.]
 [ 1.]
 [ 2.]
 [ 3.]
 [ 4.]
 [ 5.]]
The shape of x_1 changed from (11, 9) to (1, 9) and that of y_1 changed from (11, 9) to (11, 1) The indexing of Matrix is however different. Actually, it is the exact opposite of Cartesian indexing. 👁 Image
For the matrix shown above, for a given row Y-coordinate increases as 0, 1, 2, 3 from left to right whereas for a given column X-coordinate increases from top to bottom as 0, 1, 2. The two 2-dimensional arrays returned from Matrix indexing will be the transpose of the arrays generated by the previous program. The following code can be used for obtaining Matrix indexing:
Output:
x_2 = 
[[-4. -4. -4. -4. -4. -4. -4. -4. -4. -4. -4.]
 [-3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.]
 [-2. -2. -2. -2. -2. -2. -2. -2. -2. -2. -2.]
 [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1.]
 [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
 [ 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
 [ 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]]
y_2 = 
[[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]
 [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]]

True

True
The sparse = True can also be added in the meshgrid function of Matrix indexing. In this case, the shape of x_2 will change from (9, 11) to (9, 1) and that of y_2 will change from (9, 11) to (1, 11).
Comment
Article Tags:
Article Tags: