Menu Close

NumPy Meshgrid Function Explained – Python | 3D Plotting

Video Explanation

 

The numpy.meshgrid function is used to:

  • create a rectangular grid from – two “one-dimensional” arrays.

 

  • Explanation

x = [0 1 2 3 4]

y = [0 1 2 3 4 5 6 7 8 9]

X, Y = np.meshgrid(x, y)

Example

x = np.arange(0, 5)    ⇒  [0 1 2 3 4]

X, Y = np.meshgrid(x, y)   

X =

 

y = np.arange(0, 5)    ⇒  [0 1 2 3 4 5 6 7 8 9]

X, Y = np.meshgrid(x, y)   

Y =

 

⇒ 

 

3D Plotting using Meshgrid | Python

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

x = np.arange(0,50)
y = np.arange(0,100)

X,Y = np.meshgrid(x, y)

Z = np.sqrt(X**2 + Y**2)

ax.plot_surface(X, Y, Z)

plt.show()



More Related Stuff