# Numpy array append – Python NumPy meshgrid() Function

NumPy meshgrid() Function:

Numpy array append: From coordinate vectors, the NumPy meshgrid() function returns coordinate matrices.

Numpy.meshgrid: Given one-dimensional coordinate arrays x1, x2,…, xn, it generates N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids.

Syntax:

numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')

Parameters

x1, x2, …..xn: This is required. It Specifies the 1-D arrays (array-like) that represent grid coordinates.

Return Value:

NP.meshgrid: Returns (N2, N1, N3,…Nn) shaped arrays 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).

## NumPy meshgrid() Function in Python

Example1

Approach:

• Import numpy module using the import keyword.
• Give the list as static input and store it in a variable.
• Give the other list as static input and store it in another variable.
• Pass the above first and second lists as arguments to the meshgrid() function to get two 2-Dimensional(2D) arrays.
• Store them in two separate variables.
• Print the above-obtained result list1
• Print the above-obtained result list2
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the list as static input and store it in a variable.
gvn_lst1 = [1, 3, 5, 7, 9]
# Give the other list as static input and store it in another variable.
gvn_lst2 = [2, 4, 6, 8, 10]

# Pass the above first and second lists as arguments to the meshgrid() function
# to get two 2-Dimensional(2D) arrays
# Store them in two separate variables
rslt_lst1, rslt_lst2 = np.meshgrid(gvn_lst1, gvn_lst2)
# Print the above obtained result list1
print("The above obtained result list1 = ")
print(rslt_lst1)
# Print the above obtained result list1
print("The above obtained result list2 = ")
print(rslt_lst2)

Output:

The above obtained result list1 =
[[1 3 5 7 9]
[1 3 5 7 9]
[1 3 5 7 9]
[1 3 5 7 9]
[1 3 5 7 9]]
The above obtained result list2 =
[[ 2 2 2 2 2]
[ 4 4 4 4 4]
[ 6 6 6 6 6]
[ 8 8 8 8 8]
[10 10 10 10 10]]

Example2

Approach:

• Import pyplot from the matplotlib module using the import keyword
• Import numpy module using the import keyword
• Getting a and b lists using linspace() function
• Creating an elliptical plane using the mathematical formula
• Getting figure and axis values using subplot() function
• Using meshgrid and elliptical plane values, draw the filled contour plot
• Add the above colorbar to the plot using the colorbar() function
• Plotting it.
• The Exit of the Program.

Below is the implementation:

# Import pyplot from the matplotlib module using the import keyword
import matplotlib.pyplot as plt
# Import numpy module using the import keyword
import numpy as np
# Getting a and b lists using linspace() function
a_lst = np.linspace(-3.0, 4.0, 200)
b_lst = np.linspace(-3.0, 4.0, 200)
A, B = np.meshgrid(a_lst, b_lst)

# Creating elliptical plane using the mathematical formula
elip_plane = (A**2)/4 + (B**2)/9
# Getting figure and axis values using subplot() function
figure, axis = plt.subplots()

# Using meshgrid and elliptical plane values, draw the filled contour plot
color_bar = axis.contourf(A, B, elip_plane)
# Add the above colorbar to the plot using the colorbar() function
figure.colorbar(color_bar)
# Plotting it
axis.set_title('Filled Contour Plot')
axis.set_xlabel('a (cm)')
axis.set_ylabel('b (cm)')

plt.show()

Output: