The purpose of meshgrid is to create a rectangular grid out of an array of x values and an array of y values.

So, for example, if we want to create a grid where we have a point at each integer value between 0 and 4 in both the x and y directions. To create a rectangular grid, we need every combination of the x and y points.

This is going to be 25 points, right? So if we wanted to create an x and y array for all of these points, we could do the following.

x[0,0] = 0    y[0,0] = 0
x[0,1] = 1    y[0,1] = 0
x[0,2] = 2    y[0,2] = 0
x[0,3] = 3    y[0,3] = 0
x[0,4] = 4    y[0,4] = 0
x[1,0] = 0    y[1,0] = 1
x[1,1] = 1    y[1,1] = 1
...
x[4,3] = 3    y[4,3] = 4
x[4,4] = 4    y[4,4] = 4

This would result in the following x and y matrices, such that the pairing of the corresponding element in each matrix gives the x and y coordinates of a point in the grid.

x =   0 1 2 3 4        y =   0 0 0 0 0
      0 1 2 3 4              1 1 1 1 1
      0 1 2 3 4              2 2 2 2 2
      0 1 2 3 4              3 3 3 3 3
      0 1 2 3 4              4 4 4 4 4

We can then plot these to verify that they are a grid:

plt.plot(x,y, marker='.', color='k', linestyle='none')

Obviously, this gets very tedious especially for large ranges of x and y. Instead, meshgrid can actually generate this for us: all we have to specify are the unique x and y values.

xvalues = np.array([0, 1, 2, 3, 4]);
yvalues = np.array([0, 1, 2, 3, 4]);

Now, when we call meshgrid, we get the previous output automatically.

xx, yy = np.meshgrid(xvalues, yvalues)

plt.plot(xx, yy, marker='.', color='k', linestyle='none')

Creation of these rectangular grids is useful for a number of tasks. In the example that you have provided in your post, it is simply a way to sample a function (sin(x**2 + y**2) / (x**2 + y**2)) over a range of values for x and y.

Because this function has been sampled on a rectangular grid, the function can now be visualized as an "image".

Additionally, the result can now be passed to functions which expect data on rectangular grid (i.e. contourf)

Answer from Suever on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-meshgrid-function
Numpy Meshgrid function - GeeksforGeeks
May 2, 2024 - # Sample code for generation of first example import numpy as np # from matplotlib import pyplot as plt # pyplot imported for plotting graphs x = np.linspace(-4, 4, 9) # numpy.linspace creates an array of # 9 linearly placed elements between # -4 and 4, both inclusive y = np.linspace(-5, 5, 11) # The meshgrid function returns # two 2-dimensional arrays x_1, y_1 = np.meshgrid(x, y) print("x_1 = ") print(x_1) print("y_1 = ") print(y_1)
🌐
The Python Coding Book
thepythoncodingbook.com › home › blog › numpy.meshgrid(): how does it work? when do you need it? are there better alternatives?
`numpy.meshgrid()`: How does it work? When do you use it? Are there better alternatives?
March 29, 2024 - You use meshgrid() to convert the 1D vectors representing the axes into 2D arrays. You can then use those arrays in place of the x and y variables in the mathematical equation. Since X is a 2D NumPy array, you’ll get a 2D array back when you ...
Top answer
1 of 10
647

The purpose of meshgrid is to create a rectangular grid out of an array of x values and an array of y values.

So, for example, if we want to create a grid where we have a point at each integer value between 0 and 4 in both the x and y directions. To create a rectangular grid, we need every combination of the x and y points.

This is going to be 25 points, right? So if we wanted to create an x and y array for all of these points, we could do the following.

x[0,0] = 0    y[0,0] = 0
x[0,1] = 1    y[0,1] = 0
x[0,2] = 2    y[0,2] = 0
x[0,3] = 3    y[0,3] = 0
x[0,4] = 4    y[0,4] = 0
x[1,0] = 0    y[1,0] = 1
x[1,1] = 1    y[1,1] = 1
...
x[4,3] = 3    y[4,3] = 4
x[4,4] = 4    y[4,4] = 4

This would result in the following x and y matrices, such that the pairing of the corresponding element in each matrix gives the x and y coordinates of a point in the grid.

x =   0 1 2 3 4        y =   0 0 0 0 0
      0 1 2 3 4              1 1 1 1 1
      0 1 2 3 4              2 2 2 2 2
      0 1 2 3 4              3 3 3 3 3
      0 1 2 3 4              4 4 4 4 4

We can then plot these to verify that they are a grid:

plt.plot(x,y, marker='.', color='k', linestyle='none')

Obviously, this gets very tedious especially for large ranges of x and y. Instead, meshgrid can actually generate this for us: all we have to specify are the unique x and y values.

xvalues = np.array([0, 1, 2, 3, 4]);
yvalues = np.array([0, 1, 2, 3, 4]);

Now, when we call meshgrid, we get the previous output automatically.

xx, yy = np.meshgrid(xvalues, yvalues)

plt.plot(xx, yy, marker='.', color='k', linestyle='none')

Creation of these rectangular grids is useful for a number of tasks. In the example that you have provided in your post, it is simply a way to sample a function (sin(x**2 + y**2) / (x**2 + y**2)) over a range of values for x and y.

Because this function has been sampled on a rectangular grid, the function can now be visualized as an "image".

Additionally, the result can now be passed to functions which expect data on rectangular grid (i.e. contourf)

2 of 10
408

Courtesy of Microsoft Excel: 

🌐
Medium
medium.com › @heyamit10 › meshgrid-explained-in-python-a-beginners-practical-guide-77f67d8bef52
Meshgrid Explained in Python: A Beginner’s Practical Guide | by Hey Amit | Medium
January 22, 2025 - Here’s how you can generate a 2D grid using meshgrid: import numpy as np # Define the range for x and y axes x = np.linspace(-10, 10, 5) # 5 values evenly spaced between -10 and 10 y = np.linspace(-5, 5, 3) # 3 values evenly spaced between ...
Find elsewhere
🌐
w3resource
w3resource.com › numpy › snippet › understanding-numpy-meshgrid.php
Understanding numpy meshgrid for Coordinate Grid Creation
December 16, 2024 - import numpy as np # Define 1D arrays x = np.linspace(0, 5, 3) # 3 points from 0 to 5 y = np.linspace(10, 20, 4) # 4 points from 10 to 20 # Create sparse grids X, Y = np.meshgrid(x, y, sparse=True) # Print the grids print("X Grid:\n", X) print("Y Grid:\n", Y)
🌐
Udacity
udacity.com › blog › 2021 › 10 › numpy-np-meshgrid-tutorial-for-beginners.html
NumPy np.meshgrid Tutorial for Beginners | Udacity
September 27, 2022 - In the following sections, we’ll talk more about matrices and what makes meshgrid useful. A matrix is a two-dimensional grid-like arrangement of numbers. The numbers in a matrix are organized by rows and columns. Matrices allow for mathematical operations such as addition, multiplication, and raising elements to a power. Using matrices allows us to perform linear algebra, with which we can efficiently perform many computations on many numbers. In our previous example, XX and YY were matrices. ... import numpy as np array_a = [1,2,3,4] array_b = [10,20,30,40] XX,YY = np.meshgrid(array_a, array_b) XX >>> array([ [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]) YY >>> array([ [10, 10, 10, 10, 10], [20, 20, 20, 20, 20], [30, 30, 30, 30, 30], [40, 40, 40, 40, 40], [50, 50, 50, 50, 50]])
🌐
Sharp Sight
sharpsight.ai › blog › numpy-meshgrid
Numpy Meshgrid, Explained - Sharp Sight
February 6, 2024 - Numpy meshgrid is creating outputs that we could use for a Euclidean, x/y grid. So we’re providing the input values, and it’s producing outputs that can act like x and y axis values.
🌐
YouTube
youtube.com › dot physics
How to use NUMPY MESHGRID and Contour Plots in Python - YouTube
This is for future Rhett (when he forgets how to do this). Here is a super quick tutorial on meshgrids and 3d plotting.If you need my other python cheat she...
Published: October 27, 2023
🌐
Programiz
programiz.com › python-programming › numpy › methods › meshgrid
NumPy meshgrid()
The meshgrid() method takes two or more 1D arrays representing coordinate values and returns a pair of 2D arrays.
🌐
Scaler
scaler.com › home › topics › what is meshgrid function in numpy?
What is Meshgrid function in NumPy? - Scaler Topics
December 14, 2022 - The numpy.meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing, read more on Scaler Topics.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy meshgrid
NumPy Meshgrid | How does Meshgrid Function Work in NumPy?
May 18, 2023 - The program imports the “numpy” module with the alias “np” and uses the linspace() function to create two arrays, “a” and “b”. The meshgrid() function is then called with the arrays “a” and “b” as arguments, and the resulting ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Iogs-lense-training
iogs-lense-training.github.io › python-for-science › contents › python_meshgrid.html
Meshgrid for numerical computations — Python for Science / Basics 0.1 documentation
Generate Coordinate Matrices: - np.meshgrid(x, y) produces two 2D arrays, X and Y, where each element of X and Y corresponds to a grid point in the Cartesian plane.