matplotlib 3D模型绘制一朵小红花
目录
- 1. 含苞待放
- 2. 灼灼其华
前言:
在github上看到一个有趣的代码,虽然情人节已经过了两天,但还是想和大家分享^_^
1. 含苞待放
3D模型的绘制需要网格点,关于网格点的作用,在基于python,Matplotlib绘制函数的等高线与三维图像的博文中已经介绍,这里不再赘述。
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) 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 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()
2. 灼灼其华
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官方手册。
到此这篇关于matplotlib 3D模型绘制一朵小红花的文章就介绍到这了,更多相关3D模型绘制内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)