Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – numpy.meshgrid
numpy.meshgrid() is used to create coordinate matrices from coordinate vectors. It's particularly useful for creating grids for plotting, evaluating functions over 2D domains, and mathematical computations that require coordinate pairs.
Syntax
numpy.meshgrid(*xi, **kwargs)
Parameters
Meshgrid can accept the following parameters ?
x1, x2, ?, xn ? It represents the coordinates of a grid.
indexing ? It is an optional parameter which defines the Cartesian 'xy' by default and matrix 'ij' index of output.
sparse ? It is an optional parameter. If we like to use sparse grid for conserving memory, then we have to set this parameter to True. By default, it is False.
copy ? It returns a copy of the original array for conserving memory when the parameter is True. By default, it is False.
Example 1: Basic Meshgrid
Let us consider the following example ?
# Import numpy
import numpy as np
# input array
x = np.array([1, 2, 3, 4, 5])
y = np.array([11, 12, 13, 14, 15])
print("Input x :\n", x)
print("Input y :\n", y)
# meshgrid() function with sparse=True
xx, yy = np.meshgrid(x, y, sparse=True)
print("Meshgrid of X:", xx)
print("Meshgrid of Y:\n", yy)
Input x : [1 2 3 4 5] Input y : [11 12 13 14 15] Meshgrid of X: [[1 2 3 4 5]] Meshgrid of Y: [[11] [12] [13] [14] [15]]
Example 2: Dense vs Sparse Meshgrid
Let's see the difference between dense and sparse meshgrid ?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([10, 20])
# Dense meshgrid (default)
xx_dense, yy_dense = np.meshgrid(x, y)
print("Dense meshgrid XX:\n", xx_dense)
print("Dense meshgrid YY:\n", yy_dense)
print("\n" + "="*30 + "\n")
# Sparse meshgrid
xx_sparse, yy_sparse = np.meshgrid(x, y, sparse=True)
print("Sparse meshgrid XX:", xx_sparse)
print("Sparse meshgrid YY:\n", yy_sparse)
Dense meshgrid XX: [[1 2 3] [1 2 3]] Dense meshgrid YY: [[10 10 10] [20 20 20]] ============================== Sparse meshgrid XX: [[1 2 3]] Sparse meshgrid YY: [[10] [20]]
Example 3: Using with Linspace
Let us take another example. It highlights the difference between linspace and meshgrid ?
# Import numpy
import numpy as np
# linspace function
a = np.linspace(3, 4, 4)
b = np.linspace(4, 5, 6)
print("linspace of a :", a)
print("linspace of b :", b)
# meshgrid function
xa, xb = np.meshgrid(a, b)
print("Meshgrid of xa :\n", xa)
print("Meshgrid of xb :\n", xb)
linspace of a : [3. 3.33333333 3.66666667 4. ] linspace of b : [4. 4.2 4.4 4.6 4.8 5. ] Meshgrid of xa : [[3. 3.33333333 3.66666667 4. ] [3. 3.33333333 3.66666667 4. ] [3. 3.33333333 3.66666667 4. ] [3. 3.33333333 3.66666667 4. ] [3. 3.33333333 3.66666667 4. ] [3. 3.33333333 3.66666667 4. ]] Meshgrid of xb : [[4. 4. 4. 4. ] [4.2 4.2 4.2 4.2] [4.4 4.4 4.4 4.4] [4.6 4.6 4.6 4.6] [4.8 4.8 4.8 4.8] [5. 5. 5. 5. ]]
Practical Application
Here's how meshgrid is commonly used for function evaluation ?
import numpy as np
# Create coordinate arrays
x = np.linspace(-2, 2, 5)
y = np.linspace(-1, 1, 3)
# Create meshgrid
X, Y = np.meshgrid(x, y)
# Evaluate a function over the grid
Z = X**2 + Y**2
print("X coordinates:\n", X)
print("Y coordinates:\n", Y)
print("Function values (X² + Y²):\n", Z)
X coordinates: [[-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.]] Y coordinates: [[-1. -1. -1. -1. -1.] [ 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1.]] Function values (X² + Y²): [[5. 2. 1. 2. 5.] [4. 1. 0. 1. 4.] [5. 2. 1. 2. 5.]]
Conclusion
numpy.meshgrid() creates coordinate matrices from coordinate vectors, enabling efficient function evaluation over 2D grids. Use sparse=True for memory efficiency when working with large grids.
