import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 6 * np.pi, 1000)
r = 1 + 0.2 * np.sin(5 * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
plt.figure(figsize=(7, 7))
plt.plot(x, y, color='teal', linewidth=2)
for i in range(1, 6):
arc_theta = np.linspace(0, 2 * np.pi / i, 300)
arc_r = 0.8 + 0.1 * i
arc_x = arc_r * np.cos(arc_theta)
arc_y = arc_r * np.sin(arc_theta)
plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6)
plt.gca().set_aspect('equal')
plt.axis('off')
plt.title('Arc Twirl Pattern')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy: Used for numerical operations like generating a range of values for the angles and radii.
matplotlib.pyplot: Used for plotting and visualizing the pattern.
2. Define Parameters for the Arc Twirl Effect
theta = np.linspace(0, 6 * np.pi, 1000)
r = 1 + 0.2 * np.sin(5 * theta)
theta: Creates a range of 1000 values between 0 and 6 * np.pi (approximately 18.85). This is the angle parameter used to generate the spiral.
r: The radius at each angle theta is calculated. The formula 1 + 0.2 * np.sin(5 * theta) introduces oscillations to the radius as theta increases, which creates a twirling effect.
3. Convert Polar Coordinates to Cartesian Coordinates
x = r * np.cos(theta)
y = r * np.sin(theta)
x and y: Convert the polar coordinates (r, theta) into Cartesian coordinates (x, y) using the formulas:
x = r * cos(theta)
y = r * sin(theta)
These are the coordinates needed to plot the spiral pattern on a 2D graph.
4. Create the Plot
plt.figure(figsize=(7, 7))
plt.plot(x, y, color='teal', linewidth=2)
plt.figure(figsize=(7, 7)): Creates a new figure with a square aspect ratio of 7x7 inches.
plt.plot(x, y, color='teal', linewidth=2): Plots the spiral defined by the x and y values with a teal color and a line width of 2. This creates the main spiral pattern.
5. Adding Arcs with Variation
for i in range(1, 6):
arc_theta = np.linspace(0, 2 * np.pi / i, 300)
arc_r = 0.8 + 0.1 * i
arc_x = arc_r * np.cos(arc_theta)
arc_y = arc_r * np.sin(arc_theta)
plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6)
for i in range(1, 6): A loop that generates 5 additional arc patterns, each with a different radius and number of points.
arc_theta: For each arc, arc_theta is a range of angles from 0 to 2 * np.pi / i with 300 points, which defines how much of a full circle the arc will cover.
arc_r: The radius for each arc starts at 0.8 and increases by 0.1 * i in each iteration. This creates expanding arcs with each loop iteration.
arc_x and arc_y: These calculate the x and y Cartesian coordinates for the arcs.
plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6): Plots the arcs using a darkcyan color, with a line width of 1.5 and transparency (alpha=0.6) to give them a subtle appearance, allowing the main spiral to remain prominent.
6. Styling
plt.gca().set_aspect('equal')
plt.axis('off')
plt.title("Arc Twirl", fontsize=20, color='teal')
plt.gca().set_aspect('equal'): Sets the aspect ratio to "equal," meaning the units on the x and y axes are scaled equally. This ensures that the arcs and spiral remain circular rather than stretched.
plt.axis('off'): Turns off the axis lines and labels for a clean look.
plt.title("Arc Twirl", fontsize=20, color='teal'): Adds a title to the plot "Arc Twirl" in a teal color with a font size of 20.
7. Display the Plot
plt.show()
plt.show(): Displays the final plot with all the arcs and spiral patterns.
0 Comments:
Post a Comment