让我们看看怎么用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)
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',
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.05*np.sin(u2),np.ones(len(h2)))
y2 = np.outer(0.05*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')
fig.savefig('redRose.png', transparent=True)
plt.show()
|