Code Explanation:
Step 1: Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy (np): Used for numerical computations (e.g., generating angle values for the curve).
matplotlib.pyplot (plt): Used for plotting the Rose Curve.
Step 2: Define Parameters for the Rose Curve
a = 5
k = 7
a (Amplitude): Controls the size of the rose curve.
k (Frequency): Defines the number of petals in the Rose Curve.
If k is odd, the Rose Curve has k petals.
If k is even, the Rose Curve has 2k petals.
Step 3: Generate Theta Values (Angle in Radians)
theta = np.linspace(0, 2 * np.pi, 1000)
theta represents the angle in radians, ranging from 0 to 2π (a full circle).
np.linspace(0, 2 * np.pi, 1000) generates 1000 equally spaced values between 0 and 2π, ensuring a smooth curve.
Step 4: Compute r (Polar Radius) for the Rose Curve
r = a * np.cos(k * theta)
This is the polar equation of the Rose Curve:
r=a⋅cos(kθ)
a (5) scales the curve, affecting the size.
k * theta determines the oscillation frequency of the petals.
Step 5: Convert Polar Coordinates to Cartesian Coordinates
x = r * np.cos(theta)
y = r * np.sin(theta)
Since matplotlib plots in Cartesian coordinates (x, y), we convert from polar to Cartesian:
x=r⋅cos(θ)
y=r⋅sin(θ)
This transforms the radius (r) and angle (theta) into X, Y coordinates.
Step 6: Set Up the Figure for Plotting
plt.figure(figsize=(6,6), facecolor="black")
figsize=(6,6): Creates a square figure (6x6 inches).
facecolor="black": Sets the background color to black for a stylish effect.
Step 7: Plot the Rose Curve
plt.plot(x, y, color='magenta', linewidth=2)
plt.plot(x, y, color='magenta', linewidth=2)
x, y: The calculated coordinates of the Rose Curve.
color='magenta': Sets the line color to magenta (pinkish-purple).
linewidth=2: Controls the thickness of the curve.
Step 8: Hide Axes for a Clean Look
plt.axis("off")
Hides the X and Y axes to make the design look more artistic.
Step 9: Add a Title
plt.title(f"Rose Curve (k={k})", color="white", fontsize=14)
Displays a title:
Uses f"Rose Curve (k={k})" to dynamically show the value of k in the title.
color="white" makes the title white for visibility on a black background.
fontsize=14 sets the font size.
Step 10: Display the Plot
plt.show()
Displays the final Rose Curve plot.
0 Comments:
Post a Comment