Sunday, 18 May 2025

Astro Web Pattern using Python

 


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
theta = np.linspace(0, 8 * np.pi, 500)
z = np.linspace(-2, 2, 500)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
t = np.linspace(0, 2 * np.pi, 50)
r_grid = np.linspace(0.1, 2.0, 10)
T, R = np.meshgrid(t, r_grid)
X_web = R * np.cos(T)
Y_web = R * np.sin(T)
Z_web = np.sin(3 * T) * 0.1
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, color='white', lw=2)
for i in np.linspace(-2, 2, 10):
    ax.plot_surface(X_web, Y_web, Z_web + i, alpha=0.2, color='cyan', edgecolor='blue')
ax.set_title("Astro Web", fontsize=18, color='cyan')
ax.set_facecolor("black")
fig.patch.set_facecolor("black")
ax.grid(False)
ax.axis('off')
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 is used for numerical operations and generating arrays.

 matplotlib.pyplot is used for plotting graphs.

 Axes3D enables 3D plotting in Matplotlib.

 2. Generating the Spiral Coordinates

theta = np.linspace(0, 8 * np.pi, 500)  # 500 angle values from 0 to 8π

z = np.linspace(-2, 2, 500)             # 500 evenly spaced height (z-axis) values

r = z**2 + 1                            # radius grows with height (parabolic growth)

theta controls the angle of rotation (spiral).

 z gives vertical position.

 r defines the radial distance from the center (makes the spiral expand outward).

 3. Convert Polar to Cartesian Coordinates

x = r * np.sin(theta)

y = r * np.cos(theta)

Converts polar coordinates (r, θ) to Cartesian (x, y) for 3D plotting.

 4. Creating the Web Grid (Circular Mesh)

t = np.linspace(0, 2 * np.pi, 50)         # 50 angles around the circle

r_grid = np.linspace(0.1, 2.0, 10)        # 10 radii from center to edge

T, R = np.meshgrid(t, r_grid)            # Create coordinate grid for circular web

X_web = R * np.cos(T)                    # X-coordinates of circular mesh

Y_web = R * np.sin(T)                    # Y-coordinates

Z_web = np.sin(3 * T) * 0.1              # Wavy pattern for the Z (height) of the web

These lines generate a circular, web-like mesh with sinusoidal (wavy) distortion.

 5. Initialize the 3D Plot

fig = plt.figure(figsize=(10, 8))                            # Create a figure

ax = fig.add_subplot(111, projection='3d')                   # Add 3D subplot

A 3D plotting area is set up with specified size.

 6. Plot the Spiral Structure

ax.plot(x, y, z, color='white', lw=2)                        # Main spiral thread

Plots the white spiral (thread of the "web").

 7. Add Web Layers in 3D

for i in np.linspace(-2, 2, 10):                              # Position 10 web layers along z-axis

    ax.plot_surface(X_web, Y_web, Z_web + i,                 # Stack circular meshes

                    alpha=0.2, color='cyan', edgecolor='blue')

Adds 10 translucent web layers with a light sine wave pattern.

 Z_web + i moves each circular mesh vertically.

 8. Styling and Aesthetics

ax.set_title("Astro Web", fontsize=18, color='cyan')        # Title with cosmic color

ax.set_facecolor("black")                                   # Set 3D plot background

fig.patch.set_facecolor("black")                            # Set figure background

ax.grid(False)                                              # Hide grid lines

ax.axis('off')                                              # Hide axes

Enhances the sci-fi/space theme with black background and cyan highlights.

 9. Display the Plot

plt.show()

Displays the final 3D Astro Web plot.

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (161) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (254) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (225) Data Strucures (14) Deep Learning (75) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (48) 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 (197) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (12) PHP (20) Projects (32) Python (1219) Python Coding Challenge (898) Python Quiz (348) 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)