In this tutorial we are going to discuss Numpy Meshgrid. The numpy module of Python provides the meshgrid() function for creating a rectangular grid with the help of 1-D arrays that represent the Matrix indexing or Cartesian indexing. MATLAB somewhat inspires this function.

The coordinate vectors are being inputted to generate 2-D matrices, and from them, the meshgrid() function returns two coordinates which are called as coordinate matrices in mathematics.

Table of Contents

## Plotting Point on a Grid: Data Visualization of the Relationship Between X and Y

Data visualization is a great way to understand complex relationships between two sets of data. One of the most popular ways to visualize these relationships is the scatter plot, which plots points on a graph with X-axis representing one set of data and Y-axis representing another set.

Consider the graph above, which has an X-axis range of -4 to 4 and a Y-axis range of -5 to 5. As a result, the figure contains a total of (9 * 11) = 99 points, each with an X-coordinate and a Y-coordinate.

The X-coordinates of the highlighted locations are -4, -3, -2, -1, 0, 1, 2, 3, 4 for any line parallel to the X-axis.

The Y-coordinates of the marked points from bottom to top for any line parallel to the Y-axis are -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.

The numpy.meshgrid function returns two 2d-Array 2 Dimensional array that reflect all of the points’ X and Y coordinates.

## Syntax

**numpy.meshgrid****(**** *xi**,

**,**

*copy=True***,**

*sparse=False*

*indexing=’xy’***)**

### Parameters

array like **x1, x2,…, xn**

This option specifies a one-dimensional array that represents grid coordinates.

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

This is an optional argument that specifies whether output is indexed in Cartesian ‘xy’ (by default) or matrix (‘ij’).

#### sparse: bool(optional)

This is also an optional parameter. This parameter must be set to True if we need a sparse grid to save memory. It is set to False by default.

#### copy: bool(optional)

This optional argument’s purpose is to array return a copy of the original array in order to save memory. It is set to False by default.

If both the sparse and copy options are False, non-contiguous arrays will be returned. Furthermore, many elements of a broadcast array can all refer to the same memory address. If we want to write into the arrays, we must first make copies.

### Returns

X1, X2,…, XN*ndarray*

Return (N1, N2, N3,…Nn) shaped arrays if indexing=’ij’ or (N2, N1, N3,…Nn) shaped arrays if indexing=’xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2, and so on for vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) if indexing=’xy

### Example

In this example, we will import Numpy as np.

We will generate two variables, x_a and y_b, and give them the numbers 5 and 6 accordingly.

Using the linspace() function, we will form two arrays, x and y.

Will declare the variables ‘x_1’ and ‘y_1’, and the meshgrid return value will be assigned to them ()

Both the arrays ‘x’ and ‘y’ will be supplied to the function.

Finally, we will attempt to print the ‘x_1’ and ‘y_1’ values.

#### Code

```
import numpy as np
x_a = 5
y_b = 6
x = np.linspace(-1, 1, x_a)
y = np.linspace(-4, 4, y_b)
x_1, y_1 = np.meshgrid(x, y)
print("x_1 = ")
print(x_1)
print("y_1 = ")
print(y_1)
```

#### Output

Two arrays containing the coordinate lengths from the coordinate vectors are shown in the output array.

### Example

In this example,

Numpy will be imported as np.

Matplotlib will be imported.

Using pyplot as a plt.

Using the np.arange() function, we will built two arrays, x_a and y_b.

Then, declare the variables ‘x_1’ and ‘y_1’, and the meshgrid will return value assigned to them ()

Both the arrays ‘x’ and ‘y’ will be supplied to the function.

After that, we will declare a variable z and assign the np.sine() function’s return value to it.

Finally, we will use the plt.contourf command to construct contour lines and filled contours ()

#### Code

```
import numpy as np
import matplotlib.pyplot as plt
x_a = 5
y_b = 6
x = np.linspace(-1, 1, x_a)
y = np.linspace(-4, 4, y_b)
xx, yy = np.meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x, y, z)
plt.axis('scaled')
plt.show()
```

#### Output

Contour lines have been plotted in the output.

## Conclusion

In this tutorial we went over Numpy Meshgrid. The array numpy module of Python provides the meshgrid() function for creating a rectangular grid with the help of 1-D arrays that represent the Matrix indexing or Cartesian coordinate array indexing.

MATLAB somewhat inspires this function, and coordinate vectors are being inputted to generate array bidimensional matrices which return two coordinates called as coordinate matrices in mathematics.

Our goal is to make you more knowledgeable about one of many functions provided by numpy module into programming language Python.