python之定制多种彩虹色爱心
准备工作:
心形曲线的计算公式:(x^2+y^2-1)^3+x^2*y^3=0
依赖库:numpy 和 matplotlib
pip3 install numpy
pip3 install matplotlib
No module named ‘matplotlib‘ 解决办法
No module named ‘numpy‘的解决办法
源码:
1 import numpy as np 2 3 import matplotlib.pyplot as plt 4 5 x_coords = np.linspace(- 100, 100, 500) 6 7 y_coords = np.linspace(- 100, 100, 500) 8 9 points = [] 10 11 for y in y_coords: 12 13 for x in x_coords: 14 15 if ((x * 0.03) ** 2 + (y * 0.03) ** 2 - 1) ** 3 - (x * 0.03) ** 2 * (y * 0.03) ** 3 <= 0: 16 17 points.append({"x": x, "y": y}) 18 19 heart_x = list(map(lambda point: point["x"], points)) 20 21 heart_y = list(map(lambda point: point["y"], points)) 22 23 plt.scatter(heart_x, heart_y, s=10, alpha=0.5) 24 25 plt.show()
效果图:
爱心基本款-初心:
上面代码运行后:
高级爱心定制款
给爱心填充不同的颜色,只需在上面代码的 “scatter函数” 中指定 “cmap参数” 即可,如下:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5, c=range(len(heart_x)), cmap=<cmap>)
通过改变"cmap"这个参数,就可以定制不同款色的高级爱心啦!
例如:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5, c=range(len(heart_x)), cmap="autumn")
<cmap = “autumn”> 橙色-热情洋溢的她
<cmap = “spring”> 青春-充满朝气的她
<cmap = “rainbow”> 彩虹-充满绚丽幻想的她
<cmap = “cool”> 紫色-优雅宁静的她
<cmap = “magma”> 晚霞-醇厚脱俗的她
<cmap = “Reds”> 炽热-热烈奔放的她
<cmap = “viridis”> 翡翠-平静柔和的她
<cmap = “gist_rainbow”> 五彩缤纷-多姿多彩的她
Much More:总有一款适合你
values: 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, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, Vega20b_r, Vega20c, Vega20c_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, 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, spectral, spectral_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