Sunday, 11 May 2025

Petal Swirl Matrix using Python

 

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

def petal_shape(k, theta):

    return np.sin(k * theta)

k_values = np.linspace(2, 7, 20)  

theta = np.linspace(0, 2 * np.pi, 400)

theta_grid, k_grid = np.meshgrid(theta, k_values)

r = petal_shape(k_grid, theta_grid)

x = r * np.cos(theta_grid)

y = r * np.sin(theta_grid)

z = k_grid  

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

ax = fig.add_subplot(111, projection='3d')

ax.set_facecolor('black')

surface = ax.plot_surface(x, y, z, cmap='spring', edgecolor='none', alpha=0.95)

ax.set_title("Petal Swirl Matrix", color='white', fontsize=18)

ax.axis('off')

fig.colorbar(surface, shrink=0.5, aspect=10)

ax.view_init(elev=45, azim=135)

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy (as np): For numerical calculations and handling arrays.

matplotlib.pyplot (as plt): For creating visual plots (2D and 3D).

Axes3D: This is necessary for 3D plotting with matplotlib.

2. Defining the Petal Shape Function
def petal_shape(k, theta):
    return np.sin(k * theta)
petal_shape(k, theta): This function generates a petal shape for the pattern, based on the frequency parameter k and angle theta. It returns the radial distance r as a function of k and theta (sinusoidal oscillation).

3. Defining Parameters and Generating Grids
k_values = np.linspace(2, 7, 20)  
theta = np.linspace(0, 2 * np.pi, 400)
theta_grid, k_grid = np.meshgrid(theta, k_values)
k_values: Creates an array of 20 values ranging from 2 to 7 for the frequency parameter k.

theta: Creates an array of 400 equally spaced values for the angle theta from 0 to 
2π.

meshgrid: Creates a 2D grid of coordinates for theta and k that will be used to evaluate the petal_shape function over all combinations.

4. Generating the Radial Distance (r)
r = petal_shape(k_grid, theta_grid)
r: Uses the petal_shape function to calculate the radial distance for each (k, theta) pair. This produces the petal-like oscillations for different values of k.

5. Converting to Cartesian Coordinates
x = r * np.cos(theta_grid)
y = r * np.sin(theta_grid)
z = k_grid  
x and y: Convert the polar coordinates (r, theta) to Cartesian coordinates.
x=rcos(θ)
y=rsin(θ)

z: Set the z coordinate to the k values, so each petal is positioned at a different height based on k.

6. Creating the 3D Plot
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
fig: Creates a new figure with a size of 6x6 inches.
ax: Adds a 3D subplot to the figure for plotting the surface.

7. Setting Plot Aesthetics
ax.set_facecolor('black')
surface = ax.plot_surface(x, y, z, cmap='spring', edgecolor='none', alpha=0.95)
ax.set_facecolor('black'): Sets the background color of the plot to black.
plot_surface: Plots the surface with:
cmap='spring': A spring-like color map for surface coloring.
edgecolor='none': Removes the edges of the surface grid.
alpha=0.95: Sets the surface to be slightly transparent.

8. Adding Title and Customizing Axes
ax.set_title("Petal Swirl Matrix", color='white', fontsize=18)
ax.axis('off')
ax.set_title: Adds the title "Petal Swirl Matrix" in white color and with a font size of 18.
ax.axis('off'): Hides the axes for a cleaner look.

9. Adding a Colorbar
fig.colorbar(surface, shrink=0.5, aspect=10)
Adds a color bar to the plot to show the color mapping used for the surface, with shrink=0.5 to reduce its size and aspect=10 to adjust its aspect ratio.

10. Adjusting the View Angle
ax.view_init(elev=45, azim=135)
view_init: Sets the initial view of the plot, with:
elev=45: Elevation angle of 45° (view from above).
azim=135: Azimuthal angle of 135° (rotates the view around the z-axis).

11. Displaying the Plot
plt.show()
plt.show(): Renders and displays the final 3D plot.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)