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