matplotlib?3D模型繪制一朵小紅花
前言:
在github上看到一個有趣的代碼,雖然情人節(jié)已經(jīng)過了兩天,但還是想和大家分享^_^
1. 含苞待放
3D模型的繪制需要網(wǎng)格點,關(guān)于網(wǎng)格點的作用,在基于python,Matplotlib繪制函數(shù)的等高線與三維圖像的博文中已經(jīng)介紹,這里不再贅述。
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()
有關(guān)mpl_toolkits.mplot3d的使用可以參考官方文檔;
更多的顏色搭配可參考matplotlib的colormap官方手冊。
到此這篇關(guān)于matplotlib 3D模型繪制一朵小紅花的文章就介紹到這了,更多相關(guān)3D模型繪制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Anaconda3建立虛擬獨立的python2.7環(huán)境方法
今天小編就為大家分享一篇使用Anaconda3建立虛擬獨立的python2.7環(huán)境方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python selenium實現(xiàn)微博自動登錄的示例代碼
本篇文章主要介紹了Python selenium實現(xiàn)微博自動登錄的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05python?操作?mongodb?數(shù)據(jù)庫詳情
這篇文章主要介紹了python?操作?mongodb?數(shù)據(jù)庫詳情,通過鏈接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫展開內(nèi)容詳細(xì),具有一定的參考價值,需要的的小伙伴可以參考一下2022-04-04YOLOv5車牌識別實戰(zhàn)教程(五)字符分割與識別
這篇文章主要介紹了YOLOv5車牌識別實戰(zhàn)教程(五)字符分割與識別,在這個教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車牌識別,幫助你快速掌握YOLOv5車牌識別技能,需要的朋友可以參考下2023-04-04