Numpy Meshgrid function

numpy.meshgrid
numpy.meshgrid
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.]]

# Sample code for generation of first example
import numpy as np
# from matplotlib import pyplot as plt
# pyplot imported for plotting graphs
  
x = np.linspace(-4, 4, 9)
  
# numpy.linspace creates an array of
# 9 linearly placed elements between
# -4 and 4, both inclusive 
y = np.linspace(-5, 5, 11)
  
# The meshgrid function returns
# two 2-dimensional arrays 
x_1, y_1 = np.meshgrid(x, y)
  
print("x_1 = ")
print(x_1)
print("y_1 = ")
print(y_1)

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.]]

An Ellipse: [Tex]$$x_1^2+4y_1^2 = 0 $$[/Tex]
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: A Sine function: [Tex]$$\dfrac{\sin(x_1^2+y_1^2)}{x_1^2+y_1^2}[/Tex]
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:
x_1, y_1 = np.meshgrid(x, y, sparse = True)

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

# Sample code for generation of Matrix indexing
import numpy as np
  
  
x = np.linspace(-4, 4, 9)
# numpy.linspace creates an array 
# of 9 linearly placed elements between
# -4 and 4, both inclusive 
y = np.linspace(-5, 5, 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 following 2 lines check if x_2 and y_2 are the
# transposes of x_1 and y_1 respectively
print("x_2 = ")
print(x_2)
print("y_2 = ")
print(y_2)
  
# np.all is Boolean and operator; 
# returns true if all holds true.
print(np.all(x_2 == x_1.T))
print(np.all(y_2 == y_1.T))

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

sparse = True


Contact Us