×

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.tril() in Python

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.

numpy.stack() in Python

numpy.stack() in Python The numpy.stack() function joins a sequence of arrays along a new axis. Syntax numpy.stack(arrays, axis=0, out=None) Parameter arrays: This parameter represents a sequence of the array where each array have the same shape. axis: It is an...

1 minute read.

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

1 minute read.

Read numpy array

Numpy is a numerical python that deals with multi-dimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library offers a multidimensional...

3 minutes 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.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.loadtxt() Python

Python numpy.loadtxt() The loadtxt() function of Python numpy class loads the data from a text fileand provides a fast reader for simple text files. Syntax numpy.loadtxt(fname, dtype=<class'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None) Parameter fname : This parameter represents a file, filename, or...

2 minutes read.

numpy.ascontiguousarray() in Python with Example

numpy.ascontiguousarray() in Python The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order). Syntax numpy.ascontiguousarray(a, dtype=None) Parameter arr : This parameter includes the input data, in any form that can be converted to an array....

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.ndarray.flatten() in Python

numpy.ndarray.flatten() in Python The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension. Syntax ndarray.flatten(order='C') Parameter The numpy. ndarray.flatten() method consists of one parameter, which is as follows: order : This parameter can be either...

1 minute read.

numpy.column_stack() in Python

numpy.column_stack() in Python The numpy.column_stack() function stacks  the 1-D arrays as columns into a 2-D array. It takes a sequence of 1-D arrays and stacks them as columns to make a...

1 minute read.

numpy.ones() in Python

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

1 minute read.

numpy.broadcast_to() in Python

numpy.broadcast_to() in Python The numpy.broadcast_to() function broadcasts an array to a new shape. Syntax numpy.broadcast_to(array, shape, subok=False) Parameter The numpy.broadcast_to() function has three parameters which are as follows:  array: This parameter represents the array to broadcast. shape: It signifies the shape...

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.

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.asscalar() in Python

The numpy.asscalar() function converts an array of size 1 to its scalar equivalent. Syntax numpy.asscalar(a) Parameter a: This parameter represents an input array of size 1. Return This function returns a scalar representation of parameter ‘a’. The output...

1 minute read.

numpy.hstack() in Python

numpy.hstack() in Python The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise). Syntax numpy.hstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D...

1 minute read.

numpy.asarray() in Python

numpy.asarray() in Python The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,. Syntax numpy.asarray(a, dtype=None, order=None) Parameter arr : This parameter includes theinput data, in any...

1 minute 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.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.