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