How To Draw Heart In Python Without Turtle
Published October 24, 2023 by Jarvis Silva
In this article I will show you how to draw heart in python without turtle module so how it is possible without turtle, we will use numpy and matplotlib library to draw a heart so let’s see how to do it.
Numpy is a powerful library which is used for numerical computing and Matplotlib is a 2d plotting library in python so as we will use both of them so make sure you have them installed, to install use below command:
# command to install numpy
pip install numpy
# command to install matplotlib
pip install matplotlib
After installing both of the libraries, open a code editor and create a python file let’s code in it.
Python code to draw heart in python without using turtle
# importing libraries
import numpy as np
from matplotlib import pyplot as plt
# Creating equally spaced 100 data in range 0 to 2*pi
theta = np.linspace(0, 2 * np.pi, 100)
# Generating x and y data
x = 16 * ( np.sin(theta) ** 3 )
y = 13 * np.cos(theta) - 5* np.cos(2*theta) - 2 * np.cos(3*theta) - np.cos(4*theta)
# Plotting
plt.plot(x, y)
plt.title('Heart Shape')
plt.show()
Above is the complete code to draw a heart in python without using turtle, as you can we have used numpy for calculations and matplotlib for plotting the heart shape, after running this program below is the output you will get.
Also see how to Draw Square In Python Without Turtle
As you can see we successfully drawn a heart shape using matplotlib in python, I hope you were able to get the same output and found this program intresting, do share it with someone who might find it helpful. Thank you for reading, Have a nice day 😊