Sunday, 16 March 2025

Rose Curve Pattern using python

 


import numpy as np
import matplotlib.pyplot as plt
a=5
k=7
theta=np.linspace(0,2*np.pi,1000)
r=a*np.cos(k*theta)
x=r*np.cos(theta)
y=r*np.sin(theta)
plt.figure(figsize=(6,6),facecolor="black")
plt.plot(x,y,color='magenta',linewidth=2)
plt.axis('off')
plt.title(f"Rose curve pattern(k={k})",color="white",fontsize=14)
plt.show()
#source code --> clcoding.com

Code Explanation: 

Step 1: Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy (np): Used for numerical computations (e.g., generating angle values for the curve).

matplotlib.pyplot (plt): Used for plotting the Rose Curve.


Step 2: Define Parameters for the Rose Curve

a = 5  

k = 7  

a (Amplitude): Controls the size of the rose curve.

k (Frequency): Defines the number of petals in the Rose Curve.

If k is odd, the Rose Curve has k petals.

If k is even, the Rose Curve has 2k petals.


Step 3: Generate Theta Values (Angle in Radians)

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

theta represents the angle in radians, ranging from 0 to 2π (a full circle).

np.linspace(0, 2 * np.pi, 1000) generates 1000 equally spaced values between 0 and 2π, ensuring a smooth curve.


Step 4: Compute r (Polar Radius) for the Rose Curve

r = a * np.cos(k * theta)

This is the polar equation of the Rose Curve:

r=a⋅cos(kθ)

a (5) scales the curve, affecting the size.

k * theta determines the oscillation frequency of the petals.


Step 5: Convert Polar Coordinates to Cartesian Coordinates

x = r * np.cos(theta)

y = r * np.sin(theta)

Since matplotlib plots in Cartesian coordinates (x, y), we convert from polar to Cartesian:

x=r⋅cos(θ)

y=r⋅sin(θ)

This transforms the radius (r) and angle (theta) into X, Y coordinates.





Step 6: Set Up the Figure for Plotting

plt.figure(figsize=(6,6), facecolor="black")

figsize=(6,6): Creates a square figure (6x6 inches).

facecolor="black": Sets the background color to black for a stylish effect.


Step 7: Plot the Rose Curve

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

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

x, y: The calculated coordinates of the Rose Curve.

color='magenta': Sets the line color to magenta (pinkish-purple).

linewidth=2: Controls the thickness of the curve.


Step 8: Hide Axes for a Clean Look

plt.axis("off")

Hides the X and Y axes to make the design look more artistic.


Step 9: Add a Title

plt.title(f"Rose Curve (k={k})", color="white", fontsize=14)

Displays a title:

Uses f"Rose Curve (k={k})" to dynamically show the value of k in the title.

color="white" makes the title white for visibility on a black background.

fontsize=14 sets the font size.


Step 10: Display the Plot

plt.show()

Displays the final Rose Curve plot.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (145) 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 (10) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (81) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1020) Python Coding Challenge (454) Python Quiz (102) 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

Python Coding for Kids ( Free Demo for Everyone)