Matplotlib?3D?繪制小紅花原理
前言:
在上篇博文中使用了matplotlib
繪制了3D小紅花,本篇博客主要介紹一下3D小紅花的繪制原理。
1. 極坐標(biāo)系
對于極坐標(biāo)系中的一點 P ,我們可以用極徑 r 和極角 θ 來表示,記為點 P ( r , θ ) ,
使用matplotlib繪制極坐標(biāo)系:
import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': ? ? # 極徑 ? ? r = np.arange(10) ? ? # 角度 ? ? theta = 0.5 * np.pi * r ? ? fig = plt.figure() ? ? plt.polar(theta, r, c='r', marker='o', ms=3, ls='-', lw=1) ? ? # plt.savefig('img/polar1.png') ? ? plt.show()
使用matplotlib繪制極坐標(biāo)散點圖:
import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': ?? ?r = np.linspace(0, 10, num=10) ? ? theta = 2 * np.pi * r ? ? area = 3 * r ** 2 ? ? ax = plt.subplot(111, projection='polar') ? ? ax.scatter(theta, r, c=theta, s=area, cmap='hsv', alpha=0.75) ? ? # plt.savefig('img/polar2.png') ? ? plt.show()
有關(guān)matplotlib
極坐標(biāo)的參數(shù)更多介紹,可參閱官網(wǎng)手冊。
2. 極坐標(biāo)系花瓣
繪制r = s i n ( θ ) r=sin(\theta)r=sin(θ)
在極坐標(biāo)系下的圖像:
import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': ?? ?fig = plt.figure() ? ? ax = plt.subplot(111, projection='polar') ? ? ax.set_rgrids(radii=np.linspace(-1, 1, num=5), labels='') ? ? theta = np.linspace(0, 2 * np.pi, num=200) ? ? r = np.sin(theta) ? ? ax.plot(theta, r) ? ? # plt.savefig('img/polar3.png') ? ? plt.show()
以 2 π 為一個周期,增加圖像的旋轉(zhuǎn)周期:
r = np.sin(2 * theta)
繼續(xù)增加圖像的旋轉(zhuǎn)周期:
r = np.sin(3 * theta) r = np.sin(4 * theta)
然后我們可以通過調(diào)整極徑系數(shù)和角度系數(shù)來調(diào)整圖像:
import matplotlib.pyplot as plt import numpy as np if __name__ == '__main__': ?? ?fig = plt.figure() ? ? ax = plt.subplot(111, projection='polar') ? ? ax.set_rgrids(radii=np.linspace(-1, 1, num=5), labels='') ? ? theta = np.linspace(0, 2 * np.pi, num=200) ? ? r1 = np.sin(4 * (theta + np.pi / 8)) ? ? r2 = 0.5 * np.sin(5 * theta) ? ? r3 = 2 * np.sin(6 * (theta + np.pi / 12)) ? ? ax.plot(theta, r1) ? ? ax.plot(theta, r2) ? ? ax.plot(theta, r3) ? ? # plt.savefig('img/polar4.png') ? ? plt.show()
3. 三維花瓣
現(xiàn)在可以將花瓣放置在三維空間上了,根據(jù)花瓣的生成規(guī)律,其花瓣外邊緣線在一條旋轉(zhuǎn)內(nèi)縮的曲線上,這條曲線的極徑 r 隨著角度的增大逐漸變小,其高度 h 逐漸變大。
其函數(shù)圖像如下:
這樣定義就滿足前面對花瓣外邊緣曲線的假設(shè)了,即 r 遞減, h 遞增。
現(xiàn)在將其放在三維空間中:
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D if __name__ == '__main__': ? ? fig = plt.figure() ? ? ax = Axes3D(fig) ? ? # plt.axis('off') ? ? x = np.linspace(0, 1, num=30) ? ? theta = np.linspace(0, 2 * np.pi, num=1200) ? ? theta = 30 * theta ? ? x, theta = np.meshgrid(x, theta) ? ? # f is a decreasing function of theta ? ? f = 0.5 * np.pi * np.exp(-theta / 50) ? ? r = x * np.sin(f) ? ? h = x * np.cos(f) ?? ?# 極坐標(biāo)轉(zhuǎn)笛卡爾坐標(biāo) ? ? X = r * np.cos(theta) ? ? Y = r * np.sin(theta) ? ? ax = ax.plot_surface(X, Y, h, ? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.cool) ? ? # plt.savefig('img/polar5.png') ? ? plt.show()
笛卡爾坐標(biāo)系(Cartesian coordinate system)
,即直角坐標(biāo)系。
然而,上述的表達仍然沒有得到花瓣的細節(jié),因此我們需要在此基礎(chǔ)之上進行處理,以得到花瓣形狀。因此設(shè)計了一個花瓣函數(shù):
其是一個以 2 π 為周期的周期函數(shù),其值域為[ 0.5 , 1.0 ],圖像如下圖所示:
再次繪制:
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D if __name__ == '__main__': ?? ?fig = plt.figure() ? ? ax = Axes3D(fig) ? ? # plt.axis('off') ? ? x = np.linspace(0, 1, num=30) ? ? theta = np.linspace(0, 2 * np.pi, num=1200) ? ? theta = 30 * theta ? ? x, theta = np.meshgrid(x, theta) ? ? # f is a decreasing function of theta ? ? f = 0.5 * np.pi * np.exp(-theta / 50) ?? ?# 通過改變函數(shù)周期來改變花瓣的形狀 ?? ?# 改變值域也可以改變花瓣形狀 ?? ?# u is a periodic function ? ? u = 1 - (1 - np.absolute(np.sin(3.3 * theta / 2))) / 2 ? ? r = x * u * np.sin(f) ? ? h = x * u * np.cos(f) ?? ? ?? ?# 極坐標(biāo)轉(zhuǎn)笛卡爾坐標(biāo) ? ? X = r * np.cos(theta) ? ? Y = r * np.sin(theta) ? ? ax = ax.plot_surface(X, Y, h, ? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r) ? ? # plt.savefig('img/polar6.png') ? ? plt.show()
4. 花瓣微調(diào)
為了使花瓣更加真實,使花瓣的形態(tài)向下凹,因此需要對花瓣的形狀進行微調(diào),這里添加一個修正項和一個噪聲擾動,修正函數(shù)圖像為:
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D if __name__ == '__main__': ?? ?fig = plt.figure() ? ? ax = Axes3D(fig) ? ? # plt.axis('off') ? ? x = np.linspace(0, 1, num=30) ? ? theta = np.linspace(0, 2 * np.pi, num=1200) ? ? theta = 30 * theta ? ? x, theta = np.meshgrid(x, theta) ? ? # f is a decreasing function of theta ? ? f = 0.5 * np.pi * np.exp(-theta / 50) ? ? noise = np.sin(theta) / 30 ? ? # u is a periodic function ? ? u = 1 - (1 - np.absolute(np.sin(3.3 * theta / 2))) / 2 + noise ? ? # y is a correction function ? ? y = 2 * (x ** 2 - x) ** 2 * np.sin(f) ? ? r = u * (x * np.sin(f) + y * np.cos(f)) ? ? h = u * (x * np.cos(f) - y * np.sin(f)) ? ? X = r * np.cos(theta) ? ? Y = r * np.sin(theta) ? ? ax = ax.plot_surface(X, Y, h, ? ? ? ? ? ? ? ? ? ? ? ? ?rstride=1, cstride=1, cmap=plt.cm.RdPu_r) ? ? # plt.savefig('img/polar7.png') ? ? plt.show()
修正前后圖像區(qū)別對比如下:
5. 結(jié)束語
3D花的繪制主要原理是極坐標(biāo),通過正弦/余弦函數(shù)進行旋轉(zhuǎn)變形構(gòu)造,參數(shù)略微變化就會出現(xiàn)不同的花朵,有趣!
到此這篇關(guān)于Matplotlib 3D
繪制小紅花原理的文章就介紹到這了,更多相關(guān)Matplotlib 繪制小紅花內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm配置Anaconda虛擬環(huán)境全過程
這篇文章主要介紹了pycharm配置Anaconda虛擬環(huán)境全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01- Python由荷蘭數(shù)學(xué)和計算機科學(xué)研究學(xué)會的Guido van Rossum 于1990 年代初設(shè)計,作為一門叫做ABC語言的替代品。 Python提供了高效的高級數(shù)據(jù)結(jié)構(gòu),還能簡單有效地面向?qū)ο缶幊?/div> 2021-10-10
python使用WMI檢測windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法
這篇文章主要介紹了python使用WMI檢測windows系統(tǒng)信息、硬盤信息、網(wǎng)卡信息的方法,涉及Python針對系統(tǒng)信息的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05最新評論