Purpose of meshgrid in Python

In this text, we will be understanding the concept of the meshgrid command in detail and how to properly use Python’s meshgrid function. We will then also learn how to plot different 2-dimensional and 3-dimensional graphical plots with the help of the np.meshgrid() function in Python programming. So, read the text below carefully to explore new ways of coding.

Meshgrid:

Meshgrid

In this graph, if you want to mark every point, you will have to draw vertical lines about the x-axis at every marker of the x-coordinate and horizontal lines about the y-axis at every marker of the y-coordinate. The grid boxes are made after these vertical and horizontal lines intersect, creating many rectangular boxes. The meshgrid function helps in converting NumPy arrays into matrices. The np.meshgrid function is primarily used for:

  • Creating or plotting 2-dimensional functions.
  • Generating combinations of two or more functions.
import numpy as np
x=np.arange(3)
y=np.arange(3)
xv,yv=np.meshgrid(x,y)
print("xv:")
print(xv)
print("yv:")
print(yv)

Output:

xv:
[[0 1 2]
 [0 1 2]
 [0 1 2]]
yv:
[[0 0 0]
 [1 1 1]
 [2 2 2]]

We can see that the matrix xv is the collection of all the x-coordinate values in the repeated form, row wise and matrix yv is the collection of all the y-coordinate values in the repeated form, column wise.

import numpy as np
x=np.arange(3)
y=np.arange(3)
xv,yv=np.meshgrid(x,y)
print("Squaring and adding 2-d arrays:")
a=xv**2+yv**2
print(a)

Output:

Squaring and adding 2-d arrays:
[[0 1 4]
 [1 2 5]
 [4 5 8]]

Now, we can see that the np.meshgrid function performs these operations for coordinates on a grid and we require this if we want to get a 2-dimensional function in return.

2-D meshgrid:

Now, we will plot a 2-d graph with the help of the np.meshgrid function in Python. Here, we will use the np.linspace() method for creating an array. For eaxmple, the np.linspace(-2,2,100) will give 100 values between -2 and 2.

# Import the libraries
import numpy as np
from matplotlib import pyplot as plt
#create arrays
x=np.linspace(-2,2,100)
y=np.linspace(-1,1,100)
# create 2d arrays using meshgrid
xv,yv=np.meshgrid(x,y)
# create exponential function
f=np.exp(-xv**2-yv**2)
# set figure size
plt.figure(figsize=(4,1.5))
# function for 2d plotting
plt.pcolormesh(xv,yv,f)
# command for colorbar
plt.colorbar()
# show the plot
plt.show()

Output:

2-D meshgrid

Note: pcolormesh is typically the preferable function for 2d plotting.

Masks:

If you have an image and you want to look at a specific region, you can create a mask over that region. An example of a circular mask over a certain region of an image is given below.

# Import the libraries
import numpy as np
from matplotlib import pyplot as plt
#create arrays
x=y=np.linspace(-5,5,500)
# create 2d arrays using meshgrid
xv,yv=np.meshgrid(x,y)
# create exponential function
f=np.exp(xv**2+yv**2<1).astype(float)
# set figure size
plt.figure(figsize=(4,3))
# function for mask plotting
plt.pcolormesh(xv,yv,f)
# command for colorbar
plt.colorbar()
# show the plot
plt.show()

Output:

Masks in Python meshgrid

Leave a Reply

Your email address will not be published. Required fields are marked *