让我们看看怎么用python绘制一朵娇艳可爱的红玫瑰?
代码参考了浪漫的形式有100种,单身的就1种! 和 Python 绘制圆柱体(3D图)
具体如下,直接执行就好:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.font_manager import FontProperties
fig = plt.figure(figsize=(6,8))
ax = fig.gca(projection='3d')
elev = 22
azim = 2.5
ax.view_init(elev, azim) #改变绘制图像的视角,即相机的位置,azim沿着z轴旋转,elev沿着y轴
font_set = FontProperties(fname=r"C:\Windows\Fonts\simhei.TTF",size=20)
ax.text(1, -0.8, 0, '"带着一朵温柔的花~"', fontproperties=font_set)
[x, t] = np.meshgrid(np.array(range(30)) / 28.0, np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = 4+u * (x * np.cos(p) - y * np.sin(p))*3
c= cm.get_cmap('Reds')
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1, color='r', #cmap= c,可改变花朵颜色
alpha=0.8, linewidth=0, antialiased=True) #rstride和cstride为横竖方向的绘图采样步长,越小绘图越精细。
#花梗
u2 = np.linspace(0,2*np.pi,50) # 把圆分按角度为50等分
h2 = np.linspace(0,4,20) # 把高度1均分为20份, 玫瑰花梗高度范围
x2 = np.outer(0.05*np.sin(u2),np.ones(len(h2))) # x值重复20次
y2 = np.outer(0.05*np.cos(u2),np.ones(len(h2))) # y值重复20次
z2 = np.outer(np.ones(len(u2)),h2) # x,y 对应的高度
pole = ax.plot_surface(x2, y2, z2, cmap=cm.get_cmap('summer') ) #cmap=cm.get_cmap('Greens'),cmap=cm.get_cmap('summer'),color='g'
plt.axis('off')
fig.savefig('redRose.png', transparent=True)
plt.show()