How to plot the area in common between two separate surfaces using Python, NumPy, and Matplotlib. By following the code provided, we will visualize the overlapping region between the two plots and gain a deeper understanding of the process. Let’s dive in!
Content: To begin, we need to generate a paraboloid and define the lower and upper limits for the area of interest. Here’s the Python code that accomplishes this:
pythonCopy code
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
r: float = 1.5
r_min: float = 0.5 * r
r_max: float = 0.75 * r
n_points: int = 100
r: np.ndarray = np.linspace(0, r, n_points)
r_pellet: np.ndarray = np.linspace(r_min, r_max, n_points)
theta: np.ndarray = np.linspace(0, 2 * np.pi, n_points)
R, THETA = np.meshgrid(r, theta)
# Deal with figures
fig = plt.figure()
ax = fig.add_subplot(projection = "3d")
# Create projectile surface
X, Y = R * np.cos(THETA), R * np.sin(THETA)
Z = (X**2 + Y**2)
ax.plot_surface(X, Y, Z, cmap="cool", linewidth=0, antialiased=True)
# Create pellet surface
# Above lower limit
X_SUP = np.where(r_min <= X**2 + Y**2, X, np.NAN)
Y_SUP = np.where(r_min <= X**2 + Y**2, Y, np.NAN)
Z_SUP = X_SUP**2 + Y_SUP**2
# Under upper limit
X_INF = np.where(X**2 + Y**2 <= r_max, X, np.NAN)
Y_INF = np.where(X**2 + Y**2 <= r_max, Y, np.NAN)
Z_INF = X_INF**2 + Y_INF**2
ax.plot_surface(X, Y, Z_INF, cmap="hot", linewidth=0, antialiased=True)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()
To visualize the overlapping region between the two plots, we can modify the code as follows:
pythonCopy code
# Get the area in common between the two surfaces
X_INF_SUP = np.where((X**2 + Y**2 >= r_min) & (X**2 + Y**2 <= r_max), X, np.NAN)
Y_INF_SUP = np.where((X**2 + Y**2 >= r_min) & (X**2 + Y**2 <= r_max), Y, np.NAN)
Z_INF_SUP = X_INF_SUP**2 + Y_INF_SUP**2
# Plot the common area
ax.plot_surface(X_INF_SUP, Y_INF_SUP, Z_INF_SUP, cmap="viridis", linewidth=0, antialiased=True)
plt.show()
By adding these lines of code, we ensure that only the points within the specified range are included in the plot. The resulting figure will display the area in common between the two surfaces with a distinctive color map.