Sunday, 16 March 2025

Star Constellation Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

num_stars = 30  

x_vals = np.random.uniform(0, 10, num_stars)

y_vals = np.random.uniform(0, 10, num_stars)

sizes = np.random.randint(20, 100, num_stars)

num_connections = 10

connections = np.random.choice(num_stars, (num_connections, 2), replace=False)

fig, ax = plt.subplots(figsize=(8, 8), facecolor='black')

ax.set_facecolor("black")

ax.scatter(x_vals, y_vals, s=sizes, color='white', alpha=0.8)

for start, end in connections:

    ax.plot([x_vals[start], x_vals[end]], [y_vals[start], y_vals[end]], 

            color='white', linestyle='-', linewidth=0.8, alpha=0.6)

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

plt.title("Star Constellation Pattern", fontsize=14, fontweight="bold", color="white")

plt.show()

#source code --> clcoding.com


Code Explanation:

Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: Used for generating random numbers (star positions, sizes, and connections).

matplotlib.pyplot: Used for creating the star constellation plot.


Define Number of Stars

num_stars = 30  

Specifies the total number of stars in the pattern.


Generate Random Star Positions

x_vals = np.random.uniform(0, 10, num_stars)

y_vals = np.random.uniform(0, 10, num_stars)

Generates num_stars random x and y coordinates.

np.random.uniform(0, 10, num_stars): Creates values between 0 and 10 to place stars randomly in a 10x10 grid.


Assign Random Star Sizes

sizes = np.random.randint(20, 100, num_stars)

Assigns a random size to each star.

np.random.randint(20, 100, num_stars): Generates star sizes between 20 and 100, making some stars appear brighter than others.


Create Random Connections Between Stars

num_connections = 10

connections = np.random.choice(num_stars, (num_connections, 2), replace=False)

num_connections = 10: Specifies that 10 pairs of stars will be connected with lines.

np.random.choice(num_stars, (num_connections, 2), replace=False):

Randomly selects 10 pairs of star indices (from 0 to num_stars-1).

Ensures that each pair is unique (replace=False).

These pairs form the constellation-like connections.


Create the Figure and Set Background

fig, ax = plt.subplots(figsize=(6, 6), facecolor='black')

ax.set_facecolor("black")

plt.subplots(figsize=(6,6)): Creates a 6x6 inches figure.

facecolor='black': Sets the entire figure background to black.

ax.set_facecolor("black"): Ensures the plot area (inside the figure) is also black.

Plot Stars as White Dots

ax.scatter(x_vals, y_vals, s=sizes, color='white', alpha=0.8)

Plots stars using scatter():

x_vals, y_vals: Star positions.

s=sizes: Varying sizes of stars.

color='white': Stars are white.

alpha=0.8: Adds slight transparency to blend the stars naturally.


Draw Lines to Connect Some Stars (Constellation Effect)

for start, end in connections:

    ax.plot([x_vals[start], x_vals[end]], [y_vals[start], y_vals[end]], 

            color='white', linestyle='-', linewidth=0.8, alpha=0.6)

Loops through each selected star pair (start, end).

Uses ax.plot() to draw a faint white line between the two stars.

color='white': Makes lines visible on a black background.

linestyle='-': Uses solid lines.

linewidth=0.8: Thin lines for a delicate look.

alpha=0.6: Faint transparency to make lines blend smoothly.


Remove Axis Labels for a Clean Look

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

Removes x and y ticks (ax.set_xticks([]), ax.set_yticks([])) so no numerical labels are shown.

Removes plot frame (ax.set_frame_on(False)) to give a seamless night-sky effect.


Add a Title and Display the Plot

plt.title("Star Constellation Pattern", fontsize=14, fontweight="bold", color="white")

plt.show()

Adds a title "Star Constellation Pattern" in white.

plt.show() displays the final constellation pattern.


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 (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (145) 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 (10) 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 (81) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1020) Python Coding Challenge (454) Python Quiz (102) 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)