numpy.meshgrid() in Python

The numpy module of Python provides meshgrid() function for creating a rectangular grid with the help of the given 1-D arrays that represent the Matrix indexing or Cartesian indexing. MATLAB somewhat inspires the meshgrid() function. From the coordinate vectors, the meshgrid() function returns the coordinate matrices.

numpy.meshgrid()

In the above figure, the x-axis is ranging from -5 to 5, and the y-axis is ranging from -5 to 5. So, there is a total of 121 points marked in the figure, each with x-coordinate and y-coordinate. For any line parallel to the x-axis, the x-coordinates of the marked points are -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, and 5 respectively. 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, and 5 respectively.

Syntax

snippet
numpy.meshgrid(*xi, **kwargs)

Parameters

x1, x2,…, xn : array_like

This parameter defines the 1-dimensional array, which represents the coordinates of a grid.

indexing: {'xy', 'ij'}(optional)

This is an optional argument which defines the Cartesian 'xy'(by default) or matrix ('ij') indexing of output.

sparse: bool(optional)

This parameter is also optional. If we need a sparse grid for conserving memory, we have to set this parameter to True. By default, it is set to False.

copy: bool(optional)

The aim of this optional argument is that it returns a copy of the original array for conserving memory. By default, it is set to False.

If both sparse and copy parameters are set to False, then it will return non-contiguous arrays. In addition, more than one element of a broadcast array can refer to a single memory location. If we need to write into the arrays, then we have to make copies first.

Returns

X1, X2, …, Xn

The coordinate length from the coordinate vector is returned from this function.

Example 1:

snippet
import numpy as np
na, nb = (5, 3)
a = np.linspace(1, 2, na)
b = np.linspace(1, 2, nb)
xa, xb = np.meshgrid(a, b)
xa
xb

Output:

Output
array([[1. , 1.25, 1.5 , 1.75, 2. ], [1. , 1.25, 1.5 , 1.75, 2. ], [1. , 1.25, 1.5 , 1.75, 2. ]]) array([[1. , 1. , 1. , 1. , 1. ], [1.5, 1.5, 1.5, 1.5, 1.5], [2. , 2. , 2. , 2. , 2. ]])

In the above code

  • We have imported numpy with alias name np.
  • We have created two variables, i.e., na and nb, and assigned the values 5 and 3, respectively.
  • We have created two arrays, i.e., a and b using linspace() function.
  • After that, we have declared the variables 'xa' and 'xb' and assigned the returned value of meshgrid()
  • We have passed both the arrays 'a' and 'b' in the function
  • Lastly, we tried to print the value of 'xa' and 'xb'.

In the output, two arrays have been shown which contain the coordinate length from the coordinate vectors.

Example 2:

snippet
import numpy as np
na, nb = (5, 3)
a = np.linspace(1, 2, na)
b = np.linspace(1, 2, nb)
xa, xb = np.meshgrid(a, b, sparse=True)
xa
xb

Output:

Output
array([[1. , 1.25, 1.5 , 1.75, 2. ]]) array([[1. ], [1.5], [2. ]])

Example 3:

snippet
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(-10, 10, 0.1)
b = np.arange(-10, 10, 0.1)
xa, xb = np.meshgrid(a, b, sparse=True)
z = np.sin(xa**2 + xb**2) / (xa**2 + xb**2)
h = plt.contourf(a,b,z)
plt.show()

Output:

numpy.meshgrid()

In the above code

  • We have imported numpy with alias name np.
  • We have imported matplotlib.pyplot as plt.
  • We have created two arrays, i.e., a and b using np.arange() function.
  • After that, we have declared the variables 'xa' and 'xb' and assigned the returned value of meshgrid()
  • We have passed both the arrays 'a' and 'b' in the function.
  • After that, we have declared a variable z and assigned the return value of np.sine() function.
  • Lastly, we tried to draw contour lines and filled contours by using the plt.contourf()

In the output, contour lines have been plotted.

Example 4:

snippet
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(-5, 5, 5)
b = np.linspace(-5, 5, 11)
random_data = np.random.random((11, 5))
xa, xb = np.meshgrid(a, b)
plt.contourf(xa, xb, random_data, cmap = 'jet')
plt.colorbar()
plt.show()

Output:

numpy.meshgrid()

Example 5:

snippet
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(-5, 5, 5)
b = np.linspace(-5, 5, 11)
random_data = np.random.random((11, 5))
xa, xb = np.meshgrid(a, b)
sine = (np.sin(xa**2 + xb**2))/(xa**2 + xb**2)
plt.contourf(xa, xb, sine, cmap = 'jet')
plt.colorbar()
plt.show()

Output:

numpy.meshgrid()
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +