正值春节+情人节,用python画一颗动态彩虹心加上祝福语
import matplotlib.pyplot as plt
import numpy as np
import math
Reds = plt.cm.Reds
Oranges = plt.cm.Oranges
spring = plt.cm.spring
Greens = plt.cm.Greens
winter = plt.cm.winter
Blues = plt.cm.Blues
BuPu = plt.cm.BuPu
num = [-0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3]
colors = [Reds, Oranges, spring, Greens, winter, Blues, BuPu]
text = ['love', 'health', 'goodness', 'success', 'beaty', 'rich', 'Happy']
x_local = [0, 1.2, -0.8, 0.8, 0.2, -0.4, 0]
y_local = [0, 0.2, -0.8, -0.6, 0.7, 0.4, 1.6]
time = [1, 1, 1, 1, 1, 1, 3]
for i in range(0, len(num)):
t = np.linspace(0, math.pi, 1000)
x = (1+num[i])*np.sin(t)
y = np.cos(t) + np.power(x, 2.0 / 3) # 心型曲线的参数方程 power参数说明:x1的x2次方
plt.scatter(x, y, c=y, cmap=colors[i], s=40)
plt.scatter(-x, y, c=y, cmap=colors[i], s=40) # 渐变颜色曲线
plt.axis([-2, 2, -2, 2]) # 坐标轴范围
plt.text(x=x_local[i], y=y_local[i], s=text[i], ha='center', va='baseline',
fontdict=dict(fontsize=14, color='r', family='monospace', weight='bold')) # 加文字祝福
plt.axis('off') # 隐藏坐标轴
plt.pause(time[i])
plt.plot()
效果如下