How to generate an Ellipse/Ellipsoid meshgrid in numpy.
This article discusses a way to generate a uniform meshgrid for Ellipse using numpy.
For this we will be exploiting the properties of an Ellipsoid to achieve our goal. This is useful for surface plotting in 2d as well as 3d planes. This can be used to generate uniform circular meshgrids as well as Ellipsoids, all by just selecting the appropriate values.
Let’s Begin
First import the necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Now we need to define our ellipsoid parameters semi-major and semi-minor axes along, x, y and z directions.
# Define the ellipsoid parameters
a = 4 # Semi-major axis - x
b = 2 # Semi-minor axis - y
c = 1 # Semi-minor axis - z
To create a meshgrid we first generate uniform meshgrid in polar co-ordinates using the Polar angle (θ) and Azimuth angle (ψ). This will be then converted into cartesian plane using the formulas given below.
X = a * sin(θ) * cos(ψ)
Y = b* sin(θ) * sin(ψ)
Z = c * cos(θ)
# Create a mesh grid
theta = np.linspace(0, np.pi, 50) # Polar angle
phi = np.linspace(0, 2 * np.pi, 100) # Azimuthal angle
Theta, Phi = np.meshgrid(theta, phi)
# Calculate the coordinates of points on the ellipsoid surface
X = a * np.sin(Theta) * np.cos(Phi)
Y = b * np.sin(Theta) * np.sin(Phi)
Z = c * np.cos(Theta)
You can control the number of points in the mesh by specifying it in theta and phi.
(To make it circular, make a = b and you get a circular meshgrid along X and Y)
To know more about the equations, you can read the Wikipedia article here: Ellipsoid — Wikipedia.
Now we can plot the functions using surface plots in matplotlib.
# Create a 3D surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axes.set_xlim(-a-1, a+1)
ax.axes.set_ylim(-b-1, b+1)
ax.set_title('Ellipsoid Surface Plot')
plt.show()
The full code
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the ellipsoid parameters
a = 4 # Semi-major axis - x
b = 2 # Semi-minor axis - y
c = 1 # Semi-minor axis - z
# Create a mesh grid
theta = np.linspace(0, np.pi, 50) # Polar angle
phi = np.linspace(0, 2 * np.pi, 100) # Azimuthal angle
Theta, Phi = np.meshgrid(theta, phi)
# Calculate the coordinates of points on the ellipsoid surface
X = a * np.sin(Theta) * np.cos(Phi)
Y = b * np.sin(Theta) * np.sin(Phi)
Z = c * np.cos(Theta)
# Create a 3D surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axes.set_xlim(-a-1, a+1)
ax.axes.set_ylim(-b-1, b+1)
ax.set_title('Ellipsoid Surface Plot')
plt.show()
Visualizing the meshgrid
So if you’re wondering how the actual mesh looks like in the X Y plane this is how it looks.
To plot this just add these lines to your plotting code.
Z = np.ones_like(X)
plt.xlim(-a-0.5, a+0.5)
plt.ylim(-b-0.5, b+0.5)
plt.pcolormesh(X, Y, Z, color='white',edgecolor='grey', cmap='Wistia', joinstyle='bevel', linewidth=0.02)
# Set labels and title
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Mesh Plot on the x-y Plane')
# Show the plot
plt.grid(True)
plt.show()
Thanks for reading. If you’re interested in learning more about meshgrids let me know in the comments.