1. 程式人生 > >用python畫一顆彩虹色愛心送給女朋友!!!

用python畫一顆彩虹色愛心送給女朋友!!!

atp img sca str lis 編寫 .py strong lib

1.準備工作:

代碼中用到numpy和matplotlib,需要先安裝這兩個庫

pip3 install numpypip3 install matplotlib

會用到的數學公式:(x^2+y^2-1)^3+x^2*y^3=0

2.編寫代碼:

importnumpy asnp

importmatplotlib.pyplot asplt

x_coords = np.linspace(- 100, 100, 500)

y_coords = np.linspace(- 100, 100, 500)

points = []

fory iny_coords:

forx inx_coords:

if((x* 0.03)** 2+(y* 0.03)** 2- 1)** 3-(x* 0.03)** 2*(y* 0.03)** 3<= 0:

points.append({ "x": x, "y": y})

heart_x = list(map( lambdapoint: point[ "x"], points))

heart_y = list(map( lambdapoint: point[ "y"], points))

plt.scatter(heart_x, heart_y, s= 10, alpha= 0.5,c=range(len(heart_x)), cmap=<cmap>)

plt.show()

運行後上面的代碼會顯示下面的圖

技術分享圖片

這樣我們的彩虹色愛心就出來了。

用python畫一顆彩虹色愛心送給女朋友!!!