×

numpy.meshgrid() in Python

numpy.meshgrid() in Python

The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors.

Syntax

numpy.meshgrid(*xi, **kwargs)

Parameter

The numpy.meshgrid() function consists of four parameters which are as follow:

x1, x2,…, xn: This parameter signifies 1-D arrays representing the coordinates of a grid.

indexing : {‘xy’, ‘ij’}, optional It is an optional parameter representing the cartesian (‘xy’, default) or matrix indexing of output.

sparse:  It is an optional parameter which takes Boolean value. If ‘True’ is passed then a sparse grid is returned in order to conserve memory.

copy: It is an optional parameter which takes Boolean value. If ‘False’ is passed, a view into the original arrays are returned to conserve the memory.

Return

This function returns the coordinate length from coordinate vectors.

Example 1

# Program to Explain
# numpy.meshgrid() fucntion
import numpy as np
x = np.linspace(-2, 2, 9)
# numpy.linspace creates an array of
# 9 linearly placed elements between
y = np.linspace(-5, 5, 11)
# meshgrid two 2-dimensional arrays 
x_1, y_1 = np.meshgrid(x, y)
print("x_1 Matrix: ")
print(x_1)
print("y_1 Matrix: ")
print(y_1)

Output

x_1 Matrix:
[[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]
[-2.  -1.5 -1.  -0.5  0.   0.5  1.   1.5  2. ]]

y_1 Matrix:
[[-5. -5. -5. -5. -5. -5. -5. -5. -5.]
[-4. -4. -4. -4. -4. -4. -4. -4. -4.]
[-3. -3. -3. -3. -3. -3. -3. -3. -3.]
[-2. -2. -2. -2. -2. -2. -2. -2. -2.]
[-1. -1. -1. -1. -1. -1. -1. -1. -1.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]
[ 2.  2.  2.  2.  2.  2.  2.  2.  2.]
[ 3.  3.  3.  3.  3.  3.  3.  3.  3.]
[ 4.  4.  4.  4.  4.  4.  4.  4.  4.]
[ 5.  5.  5.  5.  5.  5.  5.  5.  5.]]

Example 2

# Program to explain the Matrix indexing
import numpy as np
x = np.linspace(-3, 3, 9)
y = np.linspace(-4, 4, 11)
# The meshgrid function returns
# two 2-dimensional arrays   
x_1, y_1 = np.meshgrid(x, y)
x_2, y_2 = np.meshgrid(x, y, indexing = 'ij')
# The below code checks if matrix x_2 and matrix y_2 are the
# transposes of matrix x_1 and matrix y_1
print("x_2 Matrix: ")
print(x_2)
print("y_2 Matrix: ")
print(y_2)

Output

x_2 Matrix:
[[-3.   -3.   -3.   -3.   -3.   -3.   -3.   -3.   -3.   -3.   -3.  ]
[-2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25 -2.25]
[-1.5  -1.5  -1.5  -1.5  -1.5  -1.5  -1.5  -1.5  -1.5  -1.5  -1.5 ]
[-0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75 -0.75]
[ 0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.  ]
[ 0.75  0.75  0.75  0.75  0.75  0.75  0.75  0.75  0.75  0.75  0.75]
[ 1.5   1.5   1.5   1.5   1.5   1.5   1.5   1.5   1.5   1.5   1.5 ]
[ 2.25  2.25  2.25  2.25  2.25  2.25  2.25  2.25  2.25  2.25  2.25]
[ 3.    3.    3.    3.    3.    3.    3.    3.    3.    3.    3.  ]]

y_2 Matrix:
[[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]
[-4.  -3.2 -2.4 -1.6 -0.8  0.   0.8  1.6  2.4  3.2  4. ]]

Related Topics

numpy.diagflat() in Python

numpy.diagflat() in Python The diagflat() function of Python numpy class creates a two-dimensional array with the array_like input as a diagonal to the new output array. Syntax numpy.diagflat (a, k = 0) Parameter a :...

1 minute read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.

Python NumPy Tutorial

NumPy is a scientific library in Python Programming Language. It provides objects and routines for fast operations on arrays, random simulations, statistical operations, sorting, etc. Numpy has an ndarray object which...

2 minutes read.

numpy.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

numpy.eye() in Python

numpy.eye() in Python: The eye() method of Python numpy class returns a 2-D array with ones on the diagonal and zeros elsewhere. Syntax numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C') Parameters The numpy.eye()  method consists of five parameters, which...

1 minute read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

1 minute read.

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

1 minute read.

numpy.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

1 minute read.

numpy.concatenate() in Python

numpy.concatenate() in Python The numpy.concatenate() function joins a sequence of arrays along an existing axis. Syntax numpy.concatenate((a1, a2, ...), axis=0, out=None) Parameter a1, a2, … : This parameter represents the sequence of the array where they must have the same shape,...

1 minute read.

numpy.trim_zeros() in Python

numpy.trim_zeros() in Python The numpy.trim_zeros() trims the leading and/or trailing zeros from a 1-D array or sequence. Syntax numpy.trim_zeros(filt, trim='fb') Parameter The numpy.trim_zeros() function consists of two parameters, which are as follows: filt : This parameter represents the 1-D...

1 minute read.

numpy.full() in Python

numpy.full() in Python The full() method of Python numpy class returns a new array of specified shape and type, filled with fill_value. Syntax numpy.full(shape, fill_value, dtype=None, order='C') Parameter shape:This parameter represents the number of rows order :The order parameter can...

1 minute read.

numpy.asarray_chkfinite() in Python

numpy.asarray_chkfinite() in Python The numpy.asarray_chkfinite() function converts the input to an array, checking for NaNs or Infs. Syntax numpy.asarray_chkfinite(a, dtype=None, order=None) Parameter a : This parameter represents an input data, which can be in the form of lists, tuples,...

2 minutes read.

Numpy.asmatrix() in Python with Example

Numpy.asmatrix() in Python The asmatrix() function returns the specified input as a matrix. It does not make a copy if the input is already a matrix or an ndarray. Syntax numpy.asmatrix(data, dtype=None) Parameter data  :This parameter...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.fromiter() in Python

numpy.fromiter() in Python The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count=-1) Parameter The numpy.frombuffer() method consists of three parameters, which...

1 minute read.

numpy.append() in Python

numpy.append() in Python The numpy.append() function appends the values to the end of an array. Syntax numpy.append(arr, values, axis=None) Parameter The numpy.append() function consists of three parameters, which are as follows: arr : This parameter represents the values that are...

1 minute read.

numpy.rollaxis() in Python

numpy.rollaxis() in Python The numpy.rollaxis() function rolls the given axis backwards until it lies in the specified position. Syntax numpy.rollaxis(a, axis, start=0) Parameter The numpy. rollaxis() method consists of three parameters, which are as follows: a : It represents an...

1 minute read.

numpy.atleast_1d() in Python

numpy.atleast_1d() in Python The numpy.atleast_1d() function converts the inputs to arrays with at least one dimension. Syntax numpy.atleast_1d(*arys) Parameter arys1, arys2, … : This parameter represents one or more input arrays. Return This function returns an array, or list...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.