前言1. 含苞待放2. 灼灼其华
前言在github上看到一个有趣的代码,正值情人节,分享给大家^_^
1. 含苞待放3D模型的绘制需要网格点,关于网格点的作用,在这篇博客中已经介绍,这里不再赘述。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# 将相位向后移动了6*pi
x = np.linspace(0, 1, num=30)
t = np.linspace(0, 1, num=1200) * 20 * np.pi + 4 * np.pi
x, t = np.meshgrid(x, t)
p = 0.5 * np.pi * np.exp(-t / (8 * np.pi))
# 添加边缘扰动
change = np.sin(15 * t) / 150
# 将t的参数减少,使花瓣的角度变大
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi)**4 / 2 + change
y = 2 * (x**2 - x)**2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
# PiYG_r
# RdBu_r
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h,
rstride=1, cstride=1, cmap=plt.cm.RdPu_r)
plt.savefig('img/img1.png')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# plt.axis('off')
x = np.linspace(0, 1, num=30)
t = np.linspace(0, 1, num=1200) * 50 * np.pi - 4 * np.pi
x, t = np.meshgrid(x, t)
p = 0.5 * np.pi * np.exp(-t / (8 * np.pi))
change = np.sin(20 * t) / 50
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi)**4 / 2 + change
y = 2 * (x**2 - x)**2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
ax = ax.plot_surface(r * np.cos(t), r * np.sin(t), h,
rstride=1, cstride=1, cmap=plt.cm.RdPu_r)
plt.savefig('img/img2.png')
plt.show()
有关mpl_toolkits.mplot3d的使用可以参考官方文档;
更多的颜色搭配可参考matplotlib的colormap官方手册。



