import numpy as np
import matplotlib.pyplot as plt
def spirograph(a, b, delta, num_points=1000):
t = np.linspace(0, 2 * np.pi, num_points)
x = np.sin(a * t + delta)
y = np.cos(b * t)
plt.figure(figsize=(6, 6))
plt.plot(x, y, color='purple', linewidth=1.5)
plt.axis("equal")
plt.axis("off")
plt.title("Spirograph Pattern", fontsize=14)
plt.show()
spirograph(a=5,b=4,delta=np.pi/2)
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy → Used for numerical operations and generating
arrays efficiently.
matplotlib.pyplot → Used for creating visualizations
in Python.
2. Generating Data for the Plot
theta = np.linspace(0, 2 * np.pi, 100)
r = 1 + 0.5 * np.sin(4 * theta)
np.linspace(0, 2 * np.pi, 100) → Generates 100
evenly spaced values between 0 and 2π (360°).
theta → Represents the angular values in radians,
making it perfect for a polar plot.
np.sin(4 * theta) → Calculates the sine of the
angular values. The 4 in the expression means the sine wave will complete 4
oscillations in one full rotation (2π).
r = 1 + 0.5 * sin(4 * theta) →
1 → Base radius (Constant Offset)
0.5 * sin(4 * theta) → Creates the sinusoidal
oscillations.
This makes the radius vary between 0.5 to 1.5 in a
wave-like pattern.
plt.figure(figsize=(8, 6))
plt.figure(figsize=(8, 6)) → Creates a figure of
size 8x6 inches. This helps in adjusting the dimensions for better visibility.
ax = plt.subplot(111, polar=True)
plt.subplot() → Creates a subplot inside the figure.
111 → Means 1 row, 1 column, and this is the 1st
subplot.
polar=True → Specifies that the plot will be in
polar coordinates instead of Cartesian.
ax.plot(theta, r, color='blue', linewidth=2)
ax.plot() → Plots the data on the polar plot using
the generated theta (angles) and r (radius values).
color='blue' → Makes the curve blue.
linewidth=2 → Sets the line thickness to 2 points
for clearer visibility.
ax.set_title('Basic Polar Plot with Sinusoidal
Pattern', va='bottom')
ax.set_title() → Adds a title to the polar plot.
va='bottom' → Aligns the title to the bottom of the
plot.
plt.show()
plt.show() → Displays the plot on the screen.
0 Comments:
Post a Comment