Sunday, 23 February 2025

Butterfly pattern plot using python


import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0,2*np.pi,300)

x = np.sin(t)*(np.exp(np.cos(t))-2*np.cos(4*t))  
y = np.cos(t)*(np.exp(np.cos(t))-2*np.cos(4*t))  

plt.plot(x,y,color='purple',linewidth=2)
plt.plot(-x,y,color='orange',linewidth=2)

plt.title("Beautiful butterfly pattern plot",fontsize=14)
plt.axis("off")
plt.axis("equal")
plt.show()
#source code --> clcoding.com 

Code Explanation:

Importing Necessary Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: Used to generate an array of numbers (t) and perform mathematical operations.

matplotlib.pyplot: Used to plot the butterfly pattern.

Generating Values for the Plot

t = np.linspace(0, 2*np.pi, 300)

t is a NumPy array containing 300 values evenly spaced between 0 and 2π.

These values act as the parameter for our equations.


Defining the X and Y Coordinates

x = np.sin(t) * (np.exp(np.cos(t)) - 2*np.cos(4*t))  

y = np.cos(t) * (np.exp(np.cos(t)) - 2*np.cos(4*t))  

np.sin(t) and np.cos(t) create a symmetrical shape.

These equations define a butterfly-like pattern.


Creating the Plot

plt.figure(figsize=(6, 6))

This sets the figure size to 6x6 inches.

It ensures that the butterfly shape is not stretched.


Plotting the Butterfly Wings

plt.plot(x, y, color='purple', linewidth=2)   

plt.plot(-x, y, color='orange', linewidth=2)  

The first plot plt.plot(x, y, ...) creates one wing.

The second plot plt.plot(-x, y, ...) mirrors the first wing.

Colors:

Purple for one side

Orange for the mirrored side


Adding Aesthetics

plt.title("Beautiful Butterfly Pattern ", fontsize=14)

plt.axis("equal")  # Keeps the proportions equal

plt.axis("off")    # Hides the axes for a clean look

Title makes the plot more readable.

Equal aspect ratio prevents stretching or distortion.

Axis removal ensures focus on the butterfly.


Displaying the Plot

plt.show()

This renders the butterfly 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 (188) 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) 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 (76) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1006) Python Coding Challenge (452) Python Quiz (87) 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)