import numpy as np
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