又是一年521,表白怎能少了骚操作!!!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def generate_rose():
# 生成网格坐标
[x, t] = np.meshgrid(np.linspace(0, 1, 60), np.linspace(0, 20 * np.pi, 1500))
# 计算高度
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
# 计算u
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
# 计算y
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
# 计算r
r = u * (x * np.sin(p) + y * np.cos(p))
# 计算h
h = u * (x * np.cos(p) - y * np.sin(p)) * 4.5
# 获取颜色映射
c = cm.get_cmap('Blues')
# 生成画布
fig = plt.figure(figsize=(6, 8))
# 添加3D坐标轴
ax = fig.add_subplot(projection='3d')
ax.view_init(30, 10)
# 添加文字
ax.text(1, 0.3, -0.3, 'I LOVE YOU!')
# 绘制曲面
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=10, cstride=10,
cmap=c,
alpha=0.8,
linewidth=0,
antialiased=True)
# 绘制圆柱体
u2 = np.linspace(0, 2 * np.pi, 50)
h2 = np.linspace(0, -4, 20)
x2 = np.outer(0.03 * np.sin(u2), np.ones(len(h2)))
y2 = np.outer(0.03 * np.cos(u2), np.ones(len(h2)))
z2 = np.outer(np.ones(len(u2)), h2)
pole = ax.plot_surface(x2, y2, z2, cmap=cm.get_cmap('summer'))
plt.axis('off')
plt.show()
if __name__ == '__main__':
generate_rose()
代码中的 get_cmap('Blues'),Blues可自行从以下颜色表中选择颜色
其中ax.text(1, 0.3, -0.3, 'I LOVE YOU!')
这段代码的参数如下:
1
:表示文本的x坐标。0.3
:表示文本的y坐标。-0.3
:表示文本的z坐标。在matplotlib中,z坐标用于控制图层,值越小,图层越靠后。I LOVE YOU!
:表示要添加的文本内容,可自行添加。
效果如下:
此外再附上HTML版本
<!DOCTYPE html>
<html>
<head>
<title>html渲染的3D玫瑰花)</title>
<meta charset="utf-8">
</head>
<body>
<canvas id="c" height="500" width="500"></canvas>
<script>
var b = document.body;
var c = document.getElementsByTagName('canvas')[0];
var a = c.getContext('2d');
document.body.clientWidth;
</script>
<script>
with (m = Math) C = cos, S = sin, P = pow, R = random;
c.width = c.height = f = 500;
h = -250;
function p(a, b, c) {
if (c > 60) return [S(a * 7) * (13 + 5 / (.2 + P(b * 4, 4))) - S(b) * 50, b * f + 50, 625 + C(a * 7) *
(13 + 5 / (.2 + P(b * 4, 4))) + b * 400, a * 1 - b / 2, a];
A = a * 2 - 1;
B = b * 2 - 1;
if (A * A + B * B < 1) {
if (c > 37) {
n = (j = c & 1) ? 6 : 4;
o = .5 / (a + .01) + C(b * 125) * 3 - a * 300;
w = b * h;
return [o * C(n) + w * S(n) + j * 610 - 390, o * S(n) - w * C(n) +
550 - j * 350, 1180 + C(B + A) * 99 - j * 300, .4 -
a * .1 + P(1 - B * B, -h * 6) * .15 - a * b * .4 + C(a + b) / 5 +
P(C((o * (a + 1) + (B > 0 ? w : -w)) / 25), 30) * .1 * (1 - B * B), o / 1e3 + .7 - o * w * 3e-6]
}
if (c > 32) {
c = c * 1.16 - .15;
o = a * 45 - 20;
w = b * b * h;
z = o * S(c) + w * C(c) + 620;
return [o * C(c) - w * S(c), 28 + C(B * .5) * 99 - b * b * b * 60 - z / 2 - h, z, (b * b * .3 +
P((1 - (A * A)), 7) * .15 + .3) * b, b * .7]
}
o = A * (2 - b) * (80 - c * 2);
w = 99 - C(A) * 120 - C(b) * (-h - c * 4.9) + C(P(1 - b, 7)) * 50 + c * 2;
z = o * S(c) + w * C(c) + 700;
return [o * C(c) - w * S(c), B * 99 - C(P(b, 7)) * 50 - c / 3 - z / 1.35 + 450, z, (1 - b / 1.2) * .9
+ a * .1, P((1 - b), 20) / 4 + .05]
}
}
setInterval(
'for(i=0;i<1e4;i++)if(s=p(R(),R(),i%46/.74)){z=s[2];x=~~(s[0]*f/z-h);y=~~(s[1]*f/z-h);if(!m[q=y*f+x]|m[q]>z)' +
'm[q]=z,a.fillStyle="rgb("+~(s[3]*h)+","+~(s[4]*h)+","+~(s[3]*s[3]*-80)+")",a.fillRect(x,y,1,1)}', 0)
</script>
</body>
</html>
效果如下: