编程语言
首页 > 编程语言> > 【Python】520爱她就用Python给她做一个漂亮的小心心

【Python】520爱她就用Python给她做一个漂亮的小心心

作者:互联网

今天恰逢520,于是打算用Python画个❤
(x^{2})

网上发现这个函数,该函数图像是一个爱心❤,刚好满足需要

(x^{2}+y^{2}-1)^{3}-x^{2}y^{3} = 0

开始制作

首先要有这两个包

import numpy as np
import matplotlib.pyplot as plt

接下来上代码

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x_coords = np.linspace(- 100, 100, 500)
y_coords = np.linspace(- 100, 100, 500)
points = []

for y in y_coords:
    for x in x_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(lambda point: point["x"], points))
heart_y = list(map(lambda point: point["y"], points))

plt.scatter(heart_x, heart_y, s= 10, alpha= 0.5, c=range(len(heart_x)), cmap="Greens_r")  # 其中最后一个参数cmap="XXX"可以换不同颜色的爱心
plt.show()

其中倒数第二行的cmap="XXX"里面可以换不同的爱心的颜色
 

一共有这么多种颜色的爱心:

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r
 

可以拿去玩个够,哈哈哈哈哈哈哈哈哈哈

下面生成个爱心试一试

先来个autumn色,热情豪爽暖洋洋

再来看看rainbow,彩虹色,绚丽多彩美好心情

spring,青春朝气喜洋洋

PuRd,紫红色,高贵冷艳有气质

 

有多种多样非常好看的颜色,520爱她就给她送个小心心哦

 

参考文章:https://www.sohu.com/a/232683752_453160

标签:rainbow,gist,gray,Python,漂亮,520,import,coords,100
来源: https://blog.csdn.net/CSDN_fzs/article/details/106234432