import numpy as np
import matplotlib.pyplot as plt
n=1000
golden_angle=np.pi*(3-np.sqrt(5))
theta=np.arange(n)*golden_angle
r=np.sqrt(np.arange(n))
x=r*np.cos(theta)
y=r*np.sin(theta)
plt.figure(figsize=(6,6))
plt.scatter(x,y,s=5,c=np.arange(n),cmap="viridis",alpha=0.75)
plt.axis('off')
plt.title('Sunflower spiral pattern')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries
import
numpy as np
import
matplotlib.pyplot as plt
numpy:
Used for numerical operations (angles, radius, and Cartesian coordinates).
matplotlib.pyplot:
Used to create and display the plot.
n
= 1000
n
is the total number of seeds in the pattern.
Higher
values of n produce denser spirals.
golden_angle
= np.pi * (3 - np.sqrt(5))
The
golden angle (~137.5° in radians) is derived from the golden ratio (Φ ≈ 1.618).
This
angle ensures optimal packing, just like real sunflower seeds.
theta
= np.arange(n) * golden_angle
r
= np.sqrt(np.arange(n))
theta
= np.arange(n) * golden_angle:
Each
seed is rotated by golden_angle to create a spiral effect.
r
= np.sqrt(np.arange(n)):
Controls
the radial distance of seeds.
The
square root ensures even spacing outward.
x
= r * np.cos(theta)
y
= r * np.sin(theta)
Converts
polar coordinates (r, θ) into Cartesian coordinates (x, y).
cos()
and sin() help place the seeds in a circular pattern.
plt.figure(figsize=(6,
6)) # Define figure size
plt.scatter(x,
y, s=5, c=np.arange(n), cmap="viridis", alpha=0.75)
plt.scatter(x,
y, s=5, c=np.arange(n), cmap="viridis", alpha=0.75)
x,
y: Seed positions.
s=5:
Size of each seed.
c=np.arange(n):
Color gradient based on seed index.
cmap="viridis":
Uses a color gradient.
alpha=0.75:
Sets transparency.
plt.axis("off") # Hides axes
plt.title('Sunflower
Spiral Pattern') # Adds a title
plt.axis("off"):
Removes unnecessary axes.
plt.title('Sunflower
Spiral Pattern'): Labels the figure.
plt.show()
Renders
the final visualization.


0 Comments:
Post a Comment