python画彩虹爱心_用python画一颗彩虹色爱心送给女朋友!!!
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=)
plt.show()
運行后上面的代碼會顯示下面的圖
這樣我們的彩虹色愛心就出來了。
總結
以上是生活随笔為你收集整理的python画彩虹爱心_用python画一颗彩虹色爱心送给女朋友!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可视化排班管理_小白经理的思考日记-可视
- 下一篇: where里能用max吗_超市里四五元钱