Sunday, 23 February 2025

Heart shape pattern plot using python


 import numpy as np

import matplotlib.pyplot as plt

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

x=16*np.sin(t)**3

y=13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)-np.cos(4*t)

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

plt.plot(x,y,color='red',linewidth=8)

plt.title("heartshape pattern plot",fontsize=14)

plt.axis("equal")

plt.axis("off")

plt.show()

#source code --> clcoding.com 

Code Explanation:

Step 1: Importing Required Libraries

import numpy as np

import matplotlib.pyplot as plt

NumPy (np): Used to create numerical arrays and perform mathematical operations efficiently.

Matplotlib (plt): A powerful plotting library that allows visualization of data in various formats.


Step 2: Creating the Parameter t

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

The variable t is an array of 500 evenly spaced values between 0 and 2π.

This range is crucial because it allows the parametric equations to fully define the heart shape. More points would create a smoother curve, while fewer points would make it look more jagged.


Step 3: Defining the Heart Shape Equations

x = 16 * np.sin(t)**3

y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)

These equations define the x and y coordinates of the heart shape.


Explanation of the x Equation:

x = 16 * np.sin(t)**3

np.sin(t) generates a sine wave between -1 and 1.

Cubing it (**3) amplifies the curvature and ensures the heart shape is symmetric.

The factor 16 scales the shape horizontally.


Explanation of the y Equation:

y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)

The primary component is 13 * np.cos(t), which determines the general height and shape.

The additional cosine terms (cos(2t), cos(3t), cos(4t)) refine the shape, adding the necessary dips and curves to resemble a heart.

The coefficients (13, -5, -2, -1) adjust the proportions of the lobes and the bottom curve.


Step 4: Plotting the Heart

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

plt.plot(x, y, color='red', linewidth=8)  

plt.figure(figsize=(6,6)): Creates a square figure (6x6 inches) to ensure the heart doesn’t look distorted.

plt.plot(x, y, color='red', linewidth=8):

Plots the heart shape using the x and y coordinates.

The color is set to red to resemble a traditional heart.

The linewidth is set to 8, making the heart bold and thick.


Step 5: Customizing the Display

plt.title("Heart shape pattern plot", fontsize=16)

plt.axis("equal")

plt.axis("off")

plt.title("Heart shape pattern plot", fontsize=16): Adds a title to the plot with a larger font size for visibility.

plt.axis("equal"): Ensures the aspect ratio remains 1:1, preventing distortion.

plt.axis("off"): Hides the axes, making the heart the only focus.


Step 6: Displaying the Heart

plt.show()

This renders the heart shape and displays it on the screen.



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)