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.
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.
numpy.meshgrid(*xi, **kwargs)
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.
X1, X2, …, Xn
The coordinate length from the coordinate vector is returned from this function.
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:
In the above code
In the output, two arrays have been shown which contain the coordinate length from the coordinate vectors.
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:
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:
In the above code
In the output, contour lines have been plotted.
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:
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: