import matplotlib.pyplot as plt
import numpy as np
u=np.linspace(0,2*np.pi,100)
v=np.linspace(-np.pi/2,np.pi/2,100)
u,v=np.meshgrid(u,v)
x=np.sin(u)*(1+0.5*np.cos(v))*np.cos(v)
y=np.cos(u)*(1+0.5*np.cos(v))*np.cos(v)
z=np.sin(v)+0.2*np.sin(3*u)
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
ax.plot_surface(x,y,z,cmap='coolwarm',edgecolor='k',alpha=0.9)
ax.set_title('3D Butterfly Wing')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_box_aspect([1,1,0.5])
plt.tight_layout()
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy: Used for creating numerical arrays and
trigonometric functions.
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(-np.pi / 2, np.pi / 2, 100)
u, v = np.meshgrid(u, v)
u: Controls the angular direction (think of it like
horizontal spread of the wing).
x = np.sin(u) * (1 + 0.5 * np.cos(v)) * np.cos(v)
y = np.cos(u) * (1 + 0.5 * np.cos(v)) * np.cos(v)
z = np.sin(v) + 0.2 * np.sin(3 * u)
These equations build a curved, sinusoidal surface
that resembles butterfly wings:
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
Creates a square figure with a 3D plotting
environment.
ax.plot_surface(x, y, z, cmap='coolwarm',
edgecolor='k', alpha=0.9)
plot_surface: Draws the 3D shape.
ax.set_title('3D Butterfly Wings (Mathematical
Model)', fontsize=14)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_box_aspect([1, 1, 0.5])
Adds title and axis labels.
7. Show the Plot
plt.tight_layout()
plt.show()
tight_layout(): Adjusts padding between plot
elements.


0 Comments:
Post a Comment