import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
u=np.linspace(0,2*np.pi,100)
v=np.linspace(0,2*np.pi,100)
u,v=np.meshgrid(u,v)
R=1
r=0.3
bloom=0.1*np.sin(5*u)*np.cos(7*v)
x=(R+r*np.cos(v)+bloom)*np.cos(u)
y=(R+r*np.cos(v)+bloom)*np.sin(u)
z=r*np.sin(v)+bloom
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
ax.set_facecolor('black')
surface = ax.plot_surface(x, y, z, cmap='plasma', edgecolor='none', alpha=0.95)
ax.set_xlim([-1.5,1.5])
ax.set_ylim([-1.5,1.5])
ax.set_zlim([-1.5,1.5])
ax.axis('off')
fig.colorbar(surface,shrink=0.5,aspect=10)
plt.title('Coral Torus Bloom',color='white',fontsize=18)
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy (as np): Used for numerical operations and array manipulations.
matplotlib.pyplot (as plt): Used for plotting 2D/3D graphics.
Axes3D: Enables 3D plotting in matplotlib.
2. Creating Meshgrid Parameters
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 2 * np.pi, 100)
u, v = np.meshgrid(u, v)
u and v: Arrays of 100 values evenly spaced from 0 to
2π, representing angles.
meshgrid: Creates 2D coordinate grids from u and v for parameterizing a surface.
3. Defining Torus Parameters and Surface Perturbation
R = 1
r = 0.3
bloom = 0.1 * np.sin(5 * u) * np.cos(7 * v)
R: Major radius (distance from center of tube to center of torus).
r: Minor radius (radius of the tube).
bloom: A sinusoidal perturbation to add organic "bloom" effects to the torus surface, like coral growth.
4. Parametric Equations of the Deformed Torus
x = (R + r * np.cos(v) + bloom) * np.cos(u)
y = (R + r * np.cos(v) + bloom) * np.sin(u)
z = r * np.sin(v) + bloom
These are the parametric equations of a torus modified by the bloom effect:
x=(R+rcos(v)+bloom)cos(u)
y=(R+rcos(v)+bloom)sin(u)
z=rsin(v)+bloom
5. Creating the 3D Plot
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
fig: Creates a new figure window of size 6x6 inches.
ax: Adds a 3D subplot to the figure.
6. Plot Settings and Aesthetics
ax.set_facecolor('black')
surface = ax.plot_surface(x, y, z, cmap='plasma', edgecolor='none', alpha=0.95)
ax.set_facecolor: Sets the background of the 3D plot to black.
plot_surface: Plots the deformed torus surface with:
cmap='plasma': Color map for surface coloring.
edgecolor='none': No mesh lines.
alpha=0.95: Slight transparency.
7. Setting Plot Limits and Hiding Axes
ax.set_xlim([-1.5, 1.5])
ax.set_ylim([-1.5, 1.5])
ax.set_zlim([-1.5, 1.5])
ax.axis('off')
These limit the 3D view range for x, y, z axes and hide the axis grid and ticks for a cleaner look.
8. Adding Colorbar and Title
fig.colorbar(surface, shrink=0.5, aspect=10)
plt.title("Coral Torus Bloom", color='white', fontsize=18)
Adds a color bar to indicate surface value mappings.
Sets the title of the plot with white text.
9. Displaying the Plot
plt.show()
Renders the final 3D visualization.


0 Comments:
Post a Comment