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

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:

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:

Output:

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

Example 3:

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:

Output:

numpy.meshgrid()

Example 5:

Output:

numpy.meshgrid()
Next TopicNumpy.std()




Latest Courses