Easiest Python Code to make a heart – 2024

Hey guys in this blog we will see Python Code to make a heart. This is going to be a very short and interesting blog, So without any further due, let’s do it…

Python Code to make a heart using Matplotlib

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0, 2 * np.pi, 100)
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.plot(x, y, 'r-', linewidth=2)
plt.show()
  • This program uses the matplotlib.pyplot library to create a plot, and the numpy library to create data points for the x and y coordinates of the heart shape.
  • The np.linspace() function is used to create a range of values for the variable t, and the np.sin and np.cos functions are used to calculate the x and y coordinates of the heart shape.
  • The plt.plot() function is then used to plot the x and y coordinates, and the r- argument specifies that the line should be red.
  • Finally, the plt.show() function is used to display the plot.

You can run this code in a Python environment like IDLE, Jupyter notebook or any Python IDE to see the heart shape.

Result

Python Code to make a heart

Download Python Code to make a heart

So this was a very short Python Code to make a heart. If you have any doubt regarding this, you can contact me by mail.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

Your email address will not be published. Required fields are marked *