# NumPy Meshgrid 3D

We can define a mesh grid in Python as a function capable of generating a rectangular grid when given two one-dimensional arrays. It is a useful function and is inspired by `MATLAB`.

In Python, the ability to create a mesh grid function comes with using the `NumPy` library. This tutorial discusses how to create a 3-dimensional mesh grid with the help of `NumPy` in Python.

## the `NumPy.meshgrid()` Function in Python

The `NumPy.meshgrid()` function takes in the coordinate vectors and returns a coordinate matrix with their help.

However, this function does not just work for generating two-dimensional arrays. Due to the regular updates, the `NumPy.meshgrid()` function can now take in `N` one-dimensional arrays and delivers matrices of the size `N-D`.

Webjets.io - How To Create Mindmaps

## Implement the `NumPy.meshgrid()` Function to Create a 3-Dimensional Meshgrid in Python

The `NumPy.meshgrid()` function can directly be utilized after importing the `NumPy` library to the Python code.

The following code uses the `NumPy.meshgrid()` function to create a three-dimensional mesh grid in Python.

``````import numpy as np
xa = np.linspace(0., 1., 2)
ya = np.linspace(2., 2., 2)
za = np.linspace(3., 5., 2)
out = np.meshgrid(xa, ya, za)
print(out)
``````

The above code provides the following output.

``````[array([[[0., 0.],
[1., 1.]],

[[0., 0.],
[1., 1.]]]), array([[[2., 2.],
[2., 2.]],

[[2., 2.],
[2., 2.]]]), array([[[3., 5.],
[3., 5.]],

[[3., 5.],
[3., 5.]]])]
``````
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.