import numpy as np
import matplotlib.pyplot as plt
n=5000
theta=np.linspace(0,12*np.pi,n)
r=np.linspace(0,1,n)+0.2*np.sin(6*theta)
x=r*np.cos(theta)
y=r*np.sin(theta)
colors=np.linspace(0,1,n)
plt.figure(figsize=(8,8))
plt.scatter(x,y,c=colors,cmap='viridis',s=2,alpha=0.8)
plt.axis('off')
plt.title('Peacock tail pattern',fontsize=14,fontweight='bold',color='darkblue')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is used for numerical operations such as generating arrays and applying mathematical functions.
matplotlib.pyplot is used for visualization, specifically for plotting the peacock tail pattern.
2. Defining the Number of Points
n = 5000
Sets the number of points to 5000, meaning the pattern will be composed of 5000 points.
A higher n creates a smoother and more detailed pattern.
3. Generating Angular and Radial Coordinates
theta = np.linspace(0, 12 * np.pi, n)
Creates an array of n values from 0 to 12π (i.e., multiple full circular rotations).
The linspace() function ensures a smooth transition of angles, creating a spiral effect.
r = np.linspace(0, 1, n) + 0.2 * np.sin(6 * theta)
np.linspace(0, 1, n): Generates a gradual outward movement from the center.
0.2 * np.sin(6 * theta): Adds a wave-like variation to the radial distance, creating feather-like oscillations.
4. Converting Polar Coordinates to Cartesian Coordinates
x = r * np.cos(theta)
y = r * np.sin(theta)
Converts the polar coordinates (r, θ) into Cartesian coordinates (x, y), which are required for plotting in Matplotlib.
The transformation uses:
x = r * cos(θ) → Determines the horizontal position.
y = r * sin(θ) → Determines the vertical position.
5. Assigning Colors to Points
colors = np.linspace(0, 1, n)
Generates a gradient of values from 0 to 1, which will later be mapped to a colormap (viridis).
This helps create a smooth color transition in the final pattern.
6. Creating the Plot
plt.figure(figsize=(8, 8))
Creates a figure with a square aspect ratio (8x8 inches) to ensure the spiral appears circular and not stretched.
plt.scatter(x, y, c=colors, cmap='viridis', s=2, alpha=0.8)
Uses scatter() to plot the generated (x, y) points.
c=colors: Colors the points using the gradient values generated earlier.
cmap='viridis': Uses the Viridis colormap, which transitions smoothly from dark blue to bright yellow.
s=2: Sets the size of each point to 2 pixels for fine details.
alpha=0.8: Makes points slightly transparent to enhance the blending effect.
7. Formatting the Plot
plt.axis('off')
Removes axes for a clean and aesthetic visualization.
plt.title("Peacock Tail Pattern", fontsize=14, fontweight='bold', color='darkblue')
Adds a title to the plot with:
fontsize=14: Medium-sized text.
fontweight='bold': Bold text.
color='darkblue': Dark blue text color.
8. Displaying the Plot
plt.show()
Displays the generated Peacock Tail Pattern.
0 Comments:
Post a Comment