Tuesday, 4 March 2025

Radial Starburst pattern using python

 


import numpy as np

import matplotlib.pyplot as plt

def draw_starburst(n_lines=100,radius=10):

    fig,ax=plt.subplots(figsize=(6,6))

    center_x,center_y=0,0

    angles=np.linspace(0,2*np.pi,n_lines,endpoint=False)

    for angle in angles:

        x=radius*np.cos(angle)

        y=radius*np.sin(angle)

        ax.plot([center_x,x],[center_y,y],color='black',lw=1)

    ax.set_xlim(-radius,radius)

    ax.set_ylim(-radius,radius)

    ax.set_aspect('equal')

    ax.axis('off')

    plt.title('Radial Starburst pattern',fontsize=14,fontweight='bold')

    plt.show()

draw_starburst(100,10)

#source code --> clcoding.com 

Code Explanation:

1. Import Necessary Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy is used to generate evenly spaced angles for the radial lines.

matplotlib.pyplot is used to create the plot and draw the lines.


2. Define the draw_starburst Function

def draw_starburst(n_lines=100, radius=10):

This function generates a radial starburst pattern.

n_lines → The number of lines forming the pattern (default: 100).

radius → The length of each radial line (default: 10).


3. Create a Figure and Axes

fig, ax = plt.subplots(figsize=(6, 6))

This initializes a matplotlib figure with a square aspect ratio.

The figsize=(6, 6) ensures the figure is a square, making the pattern symmetrical.


4. Define Center and Compute Angles

center_x, center_y = 0, 0  

angles = np.linspace(0, 2 * np.pi, n_lines, endpoint=False)

The center of the pattern is set to (0, 0).

np.linspace(0, 2 * np.pi, n_lines, endpoint=False) generates n_lines angles evenly spaced around a full circle (0 to 2π radians).


5. Draw Radial Lines

for angle in angles:

    x = radius * np.cos(angle)

    y = radius * np.sin(angle)

    ax.plot([center_x, x], [center_y, y], color='black', lw=1)

For each angle:

Compute the (x, y) coordinates using the unit circle formulas:

x=radius×cos(angle)

y=radius×sin(angle)

ax.plot([center_x, x], [center_y, y], color='black', lw=1) draws a line from the center to the calculated point.


6. Adjust Plot Aesthetics

ax.set_xlim(-radius, radius)

ax.set_ylim(-radius, radius)

ax.set_aspect('equal')

ax.axis('off')  

plt.title('Radial Starburst Pattern', fontsize=14, fontweight='bold')

plt.show()

ax.set_xlim(-radius, radius) and ax.set_ylim(-radius, radius) ensure the plot covers the entire starburst pattern.

ax.set_aspect('equal') maintains a 1:1 aspect ratio to prevent distortion.

ax.axis('off') hides axis labels and ticks for a clean visualization.

plt.title(...) sets the plot title with bold formatting.

plt.show() displays the plot.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) 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 (1) Data Analytics (2) data management (11) Data Science (142) 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 (9) 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 (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1014) Python Coding Challenge (452) Python Quiz (94) 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)