Sunday, 11 May 2025

Coral Torus Bloom Pattern using Python

 

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

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (150) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (216) Data Strucures (13) Deep Learning (67) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (185) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1215) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)