Home > Article > Backend Development > How to draw a heart shape in python
How to draw a heart shape in python: use matplotlib and numpy to draw a heart shape, the code is [init = np.arange(-np.pi, np.pi, 0.001);plt.fill_between(x, y, facecolor='red')].
Related learning recommendations: python tutorial
How to draw a heart shape in python:
python uses matplotlib and numpy to draw a heart shape.
import matplotlib.pyplot as plt import numpy as np #初始化数据 init = np.arange(-np.pi, np.pi, 0.001) y = np.subtract(np.multiply(2, np.cos(init)), np.cos(np.multiply(2, init))) x = np.subtract(np.multiply(2, np.sin(init)), np.sin(np.multiply(2, init))) #画图 plt.plot(x, y) plt.fill_between(x, y, facecolor='red') plt.show()
Result:
The above is the detailed content of How to draw a heart shape in python. For more information, please follow other related articles on the PHP Chinese website!