Wednesday, 26 March 2025

Spirograph Pattern using Python

 

import numpy as np

import matplotlib.pyplot as plt

def spirograph(a, b, delta, num_points=1000):

    t = np.linspace(0, 2 * np.pi, num_points)  

    x = np.sin(a * t + delta)

    y = np.cos(b * t)

    plt.figure(figsize=(6, 6))

    plt.plot(x, y, color='purple', linewidth=1.5)

    plt.axis("equal")

    plt.axis("off")

    plt.title("Spirograph Pattern", fontsize=14)

    plt.show()

spirograph(a=5,b=4,delta=np.pi/2)

#source code --> clcoding.com 

Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy → Used for numerical operations and generating arrays efficiently.

 

matplotlib.pyplot → Used for creating visualizations in Python.

 

2. Generating Data for the Plot

theta = np.linspace(0, 2 * np.pi, 100)

r = 1 + 0.5 * np.sin(4 * theta)

np.linspace(0, 2 * np.pi, 100) → Generates 100 evenly spaced values between 0 and 2π (360°).

 

theta → Represents the angular values in radians, making it perfect for a polar plot.

 

np.sin(4 * theta) → Calculates the sine of the angular values. The 4 in the expression means the sine wave will complete 4 oscillations in one full rotation (2π).

r = 1 + 0.5 * sin(4 * theta) →

1 → Base radius (Constant Offset)

0.5 * sin(4 * theta) → Creates the sinusoidal oscillations.

This makes the radius vary between 0.5 to 1.5 in a wave-like pattern.

 3. Creating the Plot

plt.figure(figsize=(8, 6))

plt.figure(figsize=(8, 6)) → Creates a figure of size 8x6 inches. This helps in adjusting the dimensions for better visibility.

 4. Setting Up the Polar Plot

ax = plt.subplot(111, polar=True)

plt.subplot() → Creates a subplot inside the figure.

111 → Means 1 row, 1 column, and this is the 1st subplot.

polar=True → Specifies that the plot will be in polar coordinates instead of Cartesian.

 5. Plotting the Data

ax.plot(theta, r, color='blue', linewidth=2)

ax.plot() → Plots the data on the polar plot using the generated theta (angles) and r (radius values).

color='blue' → Makes the curve blue.

linewidth=2 → Sets the line thickness to 2 points for clearer visibility.

 6. Adding Title

ax.set_title('Basic Polar Plot with Sinusoidal Pattern', va='bottom')

ax.set_title() → Adds a title to the polar plot.

va='bottom' → Aligns the title to the bottom of the plot.

 7. Displaying the Plot

plt.show()

plt.show() → Displays the plot 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 (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) 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 (11) 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 (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1041) Python Coding Challenge (454) Python Quiz (116) 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)