用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=<cmap>)

     plt.show()

     运行后上面的代码会显示下面的图

     这样我们的彩虹色爱心就出来了。

       

原文地址:https://www.cnblogs.com/gsd-tt/p/10527379.html