Sunday, 16 March 2025

Galaxy Swirl Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

num_stars=1000

theta=np.linspace(0,4*np.pi,num_stars)

r=np.exp(0.3*theta)

noise=np.random.normal(scale=0.2,size=(num_stars,2))

x=r*np.cos(theta)+noise[:,0]

y=r*np.sin(theta)+noise[:,-1]

plt.figure(figsize=(6,6),facecolor="black")

plt.scatter(x,y,color="white",s=2)

plt.scatter(-x,-y,color="white",s=2)

plt.axis('off')

plt.title("Galaxy swirl pattern",fontsize=14,color="white")

plt.show()

#source code --> clcoding.com


Code Explanation

Step 1: Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy (np): Used for numerical computations, like creating arrays, generating random values, and performing trigonometric calculations.

matplotlib.pyplot (plt): Used for creating and customizing the visualization.


Step 2: Define the Number of Stars

num_stars = 1000  

This sets the total number of stars that will be plotted in the galaxy.

A larger number (e.g., 5000) would create a denser galaxy.


Step 3: Generate Spiral Angle (θ)

theta = np.linspace(0, 4 * np.pi, num_stars)  

theta represents the angle in radians.

np.linspace(0, 4 * np.pi, num_stars) generates 1000 values from 0 to 4π (two full turns).

This controls the swirl or spiral effect in the galaxy.


Step 4: Compute Spiral Radius (r)

r = np.exp(0.3 * theta)  

This is the equation of a logarithmic spiral:

r=e 0.3θ

 exp(0.3 * theta) ensures that the spiral expands outward as theta increases.

The factor 0.3 controls the tightness of the swirl:

Lower values (e.g., 0.2) → Tighter spiral.

Higher values (e.g., 0.5) → More open swirl.


Step 5: Introduce Random Noise (Star Distribution)

noise = np.random.normal(scale=0.2, size=(num_stars, 2))

This adds randomness to the star positions to make them look more natural.

np.random.normal(scale=0.2, size=(num_stars, 2)):

Generates Gaussian noise with a mean of 0 and a standard deviation of 0.2.

The shape (num_stars, 2) means each star gets a random X and Y offset.

This makes the stars appear slightly scattered instead of forming a perfect spiral.


Step 6: Convert Polar Coordinates to Cartesian Coordinates

x = r * np.cos(theta) + noise[:, 0]

y = r * np.sin(theta) + noise[:, 1]

Converts the polar coordinates (r, theta) into Cartesian coordinates (x, y).

Equations:

x=r⋅cos(θ)+random noise

y=r⋅sin(θ)+random noise

The random noise slightly shifts each star away from the perfect spiral, creating a realistic galaxy.

Step 7: Set Up the Plot with a Black Background

plt.figure(figsize=(8, 8), facecolor="black")

Creates a new figure with a square aspect ratio (8×8 inches).

facecolor="black" sets the background color to black for a space-like appearance.


Step 8: Plot the Stars (White Dots)

plt.scatter(x, y, color="white", s=2)  

plt.scatter(x, y, color="white", s=2):

Plots each star as a tiny white dot.

s=2: Controls the size of each star (can be increased for a brighter look).


Step 9: Create a Symmetric Swirl

plt.scatter(-x, -y, color="white", s=2)

This mirrors the swirl in the opposite direction.

Instead of just x, y, we plot -x, -y to create a symmetric pattern, making the galaxy more balanced.


Step 10: Hide the Axes for a Clean Look

plt.axis("off")

Removes the X and Y axes, making the visualization look more like deep space.


Step 11: Add a Title

plt.title("Galaxy Swirl Pattern", fontsize=14, color="white")

Adds a title "Galaxy Swirl Pattern" in white color.


Step 12: Display the Final Plot

plt.show()

Renders the final galaxy swirl on the screen.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (145) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (10) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (81) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1020) Python Coding Challenge (454) Python Quiz (102) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)