Monday, 3 March 2025

Peacock tail pattern using python

 


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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)