用python做四种花3D效果(保证可运行)

tech2024-11-19  18

四种花朵三D效果

python运行环境配置请参考博客:https://blog.csdn.net/lwl223442/article/details/101169348

注:

如果出现  no model of   "matplotlib"、"numpy"、"mpl_toolkits"错误请在windows命令提示符中运行pip install   XXX来安装工具模块。

完整程序如下:

# !usr/bin/env python # -*- coding:utf-8 -*- __author__ = "" from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator import matplotlib.pyplot as plt import numpy as np

#  第一种

fig = plt.figure() ax = fig.gca(projection='3d') [x, t] = np.meshgrid(np.array(range(25))/24.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)) surf = ax.plot_surface(r*np.cos(t), r*np.sin(t), u*(x*np.cos(p)-y*np.sin(p)), rstride=1, cstride=1, cmap=cm.gist_rainbow_r,                        linewidth=0, antialiased=True) #   plt.show()

 

 

#  第二种 fig = plt.figure() ax = fig.gca(projection='3d') # 将相位向后移动了6*pi [x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 20 * np.pi + 4*np.pi) p = (np.pi / 2) * 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)) h = u * (x * np.cos(p) - y * np.sin(p)) c= cm.get_cmap('Reds') surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,                        cmap= c, linewidth=0, antialiased=True) #  plt.show()

 

#  第三种

fig = plt.figure() ax = fig.gca(projection='3d') [x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 30 * np.pi - 4*np.pi) p = (np.pi / 2) * 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)) c= cm.get_cmap('magma') surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,                        cmap= c, linewidth=0, antialiased=True) #  plt.show()

 

#  第四种

fig = plt.figure() ax = fig.gca(projection='3d') [x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 6 * np.pi - 4*np.pi) p = (np.pi / 2) * np.exp(-t / (8 * np.pi)) change = np.sin(10*t)/20 u = 1 - (1 - np.mod(5.2 * 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)) c= cm.get_cmap('spring_r') surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,                        cmap= c, linewidth=0, antialiased=True) plt.show()   #作图

 

运行效果如下 :

第一种

 第二种

第三种 

第四种

 

最新回复(0)