Monday, 28 April 2025

Sunflower Spiral pattern using python

 

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.

 2. Define Number of Points (Seeds)

n = 1000

n is the total number of seeds in the pattern.

Higher values of n produce denser spirals.

 3. Calculate the Golden Angle

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.

 4. Generate Angular and Radial Coordinates

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.

 5. Convert to Cartesian Coordinates

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.

 6. Plot the Spiral

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.

 7. Remove Axes and Add Title

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.

 8. Display the Plot

plt.show()

Renders the final visualization.

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) 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 (217) Data Strucures (13) Deep Learning (68) 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 (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) 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)