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.pyplotlibrary to create a plot, and the
numpylibrary to create data points for the x and y coordinates of the heart shape.
np.linspace()function is used to create a range of values for the variable
t, and the
np.cosfunctions are used to calculate the x and y coordinates of the heart shape.
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.
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…