Numpy Meshgrid function



Consider the above figure with an X-axis ranging from -4 to 4 and a Y-axis ranging from -5 to 5. So a total of (9 * 11) = 99 points are marked in the figure, each of which has X-coordinate and Y-coordinate. For any line parallel to the X axis, the X coordinates of the marked points are respectively -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. numpy.meshgrid numpy.meshgrid returns two two-dimensional arrays representing the X and Y coordinates of all points.

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. , four.], [- 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 code:

# Sample code to generate the first example

import numpy as np

# from matplotlib import pyplot as plt
# pyplot is imported for plotting

 

x = np.linspace ( - 4 , 4 , 9 )

  
# numpy.linspace creates an array
# 9 linearly located elements between
# -4 and 4, both inclusive

y = np.linspace ( - 5 , 5 , 11 )

  
# Grid function returns
# two 2D arrays

x_1, y_1 = np.meshgrid (x, y)

 

print ( " x_1 = " )

print (x_1)

print ( "y_1 =" )

print (y_1)

Exit:

 x_1 = [[-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.] [-4. -3. -2. -one. 0. 1. 2. 3. 4.]] y_1 = [[-5. -five. -five. -five. -five. -five. -five. -five. -5.] [-4. -four. -four. -four. -four. -four. -four. -four. -4.] [-3. -3. -3. -3. -3. -3. -3. -3. -3.] [-2. -2. -2. -2. -2. -2. -2. -2. -2.] [-1. -one. -one. -one. -one. -one. -one. -one. -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 . 4.] [5. 5. 5. 5. 5. 5. 5. 5. 5.]] 

Grid coordinates can also be used to plot functions in a given coordinate range.


Ellipse:

   

ellipse = xx * 2 + 4 * yy * * 2

plt.contourf (x_1, y_1, ellipse, cmap = `jet` )

 
plt.colorbar ()
plt.show ()

Output:

Random data:

random_data = np.random.random (( 11 , 9 ))

plt.contourf (x_1, y_1, random_data, cmap = ` jet`

 
plt.colorbar ()
plt.show ()

Output:

Sine function:

sine = (np.sin (x_1 * * 2 + y_1 * * 2 )) / (x_1 * * 2 + y_1 * * 2 )

plt.contourf (x_1, y_1, sine, cmap = `jet`

  
plt.colorbar ()
plt.show ()

Output:

We can see that x_1 is a repetition matrix and y_1 is a repetition matrix. One row x_1 and one column y_1 is enough to determine the positions of all points, since other values ​​will be repeated over and over. So we can edit the above code like this:
x_1, y_1 = np.meshgrid (x, y, sparse = True)

This will give the following output :

 x_1 = [[-4. -3. -2. -one. 0. 1. 2. 3. 4.]] y_1 = [[-5.] [-4.] [-3.] [-2.] [-1.] [0.] [1.] [2 .] [3.] [4.] [5.]] 

The shape of x_1 has changed from (11, 9) to (1, 9), and the shape of y_1 has changed from (11, 9) to ( 11, 1)

The Indexing of the Matrix, however, is different. In fact, it is the exact opposite of Cartesian indexing. 

For the matrix shown above, for a given row, the Y-coordinate is incremented as 0, 1, 2, 3 from left to right, while for a given column, the X coordinate increases from top to bottom as 0, 1, 2.
The two 2D arrays returned from the matrix index will transpose the arrays generated by the previous program. The following code can be used to get the matrix indexing:

Exit:

 x_2 = [[-4. -four. -four. -four. -four. -four. -four. -four. -four. -four. -4.] [-3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.] [-2. -2. -2. -2. -2. -2. -2. -2. -2. -2. -2.] [-1. -one. -one. -one. -one. -one. -one. -one. -one. -one. -1.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [1. 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. 4.]] y_2 = [[-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.] [-5. -four. -3. -2. -one. 0. 1. 2. 3. 4. 5.]] True True 

sparse = True can also be added to the grid function of the matrix indexing grid. In this case, x_2 will change from (9, 11) to (9, 1), and y_2 will change from (9, 11) to (1, 11).


# Example code to generate matrix index

import numpy as np

 

 

x = np.linspace ( - 4 , 4 , 9 )

# numpy.linspace creates an array
Number of 9 linear elements between
# -4 and 4, both inclusive

y = np.linspace ( - 5 , 5 , 11 )

 
# Grid function returns
# two 2D arrays

x_1, y_1 = np.meshgrid (x, y)

 

 

x_2, y_2 = np.meshgrid (x, y, indexing = `ij` )

  
# The next 2 lines check if x_2 and y are _2
# transpose x_1 and y_1 respectively

print ( "x_2 =" )

print (x_2)

print ( " y_2 = " )

print (y_2)

 
# np.all is a boolean value and operator;
# returns true if everything is correct.

print (np. all (x_2 = = x_1.T))

print (np. all (y_2 = = y_1.T))