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