新聞中心
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在python中使用mayavi實現(xiàn)三維繪圖,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)主營睢寧縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,睢寧縣h5成都小程序開發(fā)搭建,睢寧縣網(wǎng)站營銷推廣歡迎睢寧縣等地區(qū)企業(yè)咨詢Python主要用來做什么Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。
import mayavi.mlab as mlab from numpy import exp,sin,cos,tan,random,mgrid,ogrid,linspace,sqrt,pi import numpy as np import matplotlib.pyplot as plt mlab.figure(fgcolor=(0, 0, 0), bgcolor=(1, 1, 1)) #更改背景色 #添加matlab的peaks函數(shù) def peaks(x,y): return 3.0*(1.0-x)**2*exp(-(x**2) - (y+1.0)**2) - 10*(x/5.0 - x**3 - y**5) * exp(-x**2-y**2) - 1.0/3.0*exp(-(x+1.0)**2 - y**2)
首先從幫助手冊上了解下mayavi的colormap,如下圖:
下面列舉常用的三維繪圖函數(shù)和簡單例子。
一、barchart
* barchart(s, ...)
* barchart(x, y, s, ...)
* barchart(x, y, f, ...)
* barchart(x, y, z, s, ...)
* barchart(x, y, z, f, ...)
如果只傳遞一個參數(shù),可以是一維(1-D),二維(2-D)或3維(3-D)的給定向量長度的數(shù)組;
如果傳遞三個參數(shù)(x,y,s)或(x,y,f),x,y是對應(yīng)于數(shù)組s的二維(2-D)坐標(biāo),也可以是可調(diào)用的函數(shù)f,該函數(shù)返回數(shù)組;
四個參數(shù)的時候(x,y,z)表示三維坐標(biāo)
s = np.random.rand(3,3) mlab.barchart(s) mlab.vectorbar() mlab.show()
x,y = np.mgrid[-5:5:20j,-5:5:20j] s = peaks(x,y) #peaks函數(shù)前面已經(jīng)定義 mlab.barchart(x,y,s) mlab.vectorbar() mlab.show()
二、contour3d
* contour3d(scalars, ...)
* contour3d(x, y, z, scalars, ...)
* contour3d(x, y, z, f, ...)
scalars是三維數(shù)組(3-D),x,y,z用numpy.mgrid生成,是三維數(shù)組
x, y, z = ogrid[-5:5:64j, -5:5:64j, -5:5:64j] scalars = x * x * 0.5 + y * y + z * z * 2.0 mlab.contour3d(scalars, contours=6, transparent=True) mlab.colorbar() mlab.show()
三、contour_surf
* contour_surf(s, ...)
* contour_surf(x, y, s, ...)
* contour_surf(x, y, f, ...)
s是二維數(shù)組,f是可調(diào)用的函數(shù),例如peaks函數(shù)
x and y can be 1D or 2D arrays (such as returned by numpy.ogrid or numpy.mgrid)
x,y = np.mgrid[-5:5:70j,-5:5:70j] #繪制peaks函數(shù)的等高線 mlab.contour_surf(x,y,peaks,contours=9) mlab.colorbar() mlab.show()
四、imshow
* imshow(s, ...)
s is a 2 dimension array. The values of s are mapped to a color using the colormap. s = np.random.rand(3,3) #生成隨機的3×3數(shù)組 mlab.imshow(s) mlab.colorbar() mlab.show()
五、mesh
* mesh(x, y, z, ...)
x, y, z are 2D arrays, all of the same shape, giving the positions of the vertices of the surface.
x , y , z 都是二維數(shù)組,擁有相同的shape,而且z代表了平面坐標(biāo)(x,y)對應(yīng)下的值,下面繪制的是matlab的peaks函數(shù)三維圖,可能是因為繪圖比例的原因看起來并沒有matlab下繪制的好看
y,x = np.mgrid[-5:5:70j,-5:5:70j] z=peaks(x,y) mlab.mesh(x,y,z) mlab.colorbar() mlab.show()
六、surf
* surf(s, ...)
* surf(x, y, s, ...)
* surf(x, y, f, ...)
x , y可以是1-D或者2-D的數(shù)組(比如numpy.ogrid或numpy.mgrid返回的數(shù)組)
如果只傳遞了參數(shù)數(shù)組s,那么x,y就被認為是數(shù)組s的索引值,并且創(chuàng)建等寬的數(shù)據(jù)集。(If only 1 array s is passed, the x and y arrays are assumed to be made from the indices of arrays, and an uniformly-spaced data set is created.)
surf和mesh的不同之處在于surf的參數(shù)x,y可以是一維(1-D)的。
mlab.clf() x, y = mgrid[-10:10:100j, -10:10:100j] r = sqrt(x**2 + y**2) z = sin(r)/r # mlab.surf(x,y,z,wrap_scale='auto') mlab.surf(z, warp_scale='auto') mlab.colorbar() mlab.show()
surf函數(shù)同樣可以繪制peaks曲面,
pk_y,pk_x = np.mgrid[-5:5:70j,-5:5:70j] pk_z=peaks(pk_x,pk_y) mlab.surf(pk_z,warp_scale='auto',colormap='jet') mlab.colorbar() mlab.show()
這里只傳遞了一個參數(shù)pk_z,
七、plot3d
* plot3d(x, y, z, ...)
* plot3d(x, y, z, s, ...)
數(shù)據(jù)點之間繪制線段,x,y,z,s都是具有相同shape的numpy數(shù)組或列表(list),x,y,z是三維坐標(biāo),也就是空間中數(shù)據(jù)點的位置
t=mgrid[-pi:pi:100j] mlab.plot3d(cos(t),sin(3*t),cos(5*t),color=(0.23,0.6,1),colormap='Spectral') mlab.colorbar() mlab.show()
八、points3d
* points3d(x, y, z...)
* points3d(x, y, z, s, ...)
* points3d(x, y, z, f, ...)
和前面的plot3d差不多,只不過points3d只繪制三維坐標(biāo)下的點(x,y,z),仍然用前面的例子。
t=mgrid[-pi:pi:50j] s=sin(t) # 參數(shù)s是設(shè)置每個點的大小(scalar),mode可選 mlab.points3d(cos(t),sin(3*t),cos(5*t),s,mode='sphere',line_width=1) mlab.colorbar() mlab.show()
參數(shù)的mode可選項如下圖:
九、quiver3d
* quiver3d(u, v, w, ...)
* quiver3d(x, y, z, u, v, w, ...)
* quiver3d(x, y, z, f, ...)
x,y,z=mgrid[-0:3:0.6,-0:3:0.6,0:3:0.3] r=sqrt(x**2+y**2+z**4) u=y*sin(r)/(r+0.001) v=-x*sin(r)/(r+0.001) w=zeros_like(r) mlab.quiver3d(x,y,z,u,v,w) mlab.colorbar() mlab.show()
十、animate
繪制三維動圖,幫助文檔上的代碼執(zhí)行后并沒有動畫效果,下面2個示例代碼是查看了mayavi的相關(guān)源碼后總結(jié)的,大家也可以直接查看相關(guān)源碼查看更多官方提供的示例代碼。
(1)
@animate(delay=200) # 設(shè)置延時時間200ms def anim(): n_mer, n_long = 6, 11 pi = numpy.pi dphi = pi/1000.0 phi = numpy.arange(0.0, 2 * pi + 0.5 * dphi, dphi, 'd') mu = phi * n_mer x = numpy.cos(mu) * (1+numpy.cos(n_long * mu/n_mer) * 0.5) y = numpy.sin(mu) * (1+numpy.cos(n_long * mu/n_mer) * 0.5) z = numpy.sin(n_long * mu/n_mer) * 0.5 l = plot3d(x, y, z, numpy.sin(mu), tube_radius=0.025, colormap='Spectral') ms = l.mlab_source for i in range(100): x = numpy.cos(mu) * (1+numpy.cos(n_long * mu/n_mer + numpy.pi * (i+1)/5.) * 0.5) scalars = numpy.sin(mu + numpy.pi * (i+1)/5) #不改變shape和size的情況下用set來更改屬性值 ms.set(x=x, scalars=scalars) yield anim() show()
(2)
@animate #默認500ms延時 def anim2(): x, y = np.mgrid[0:3:1,0:3:1] s = mlab.surf(x, y, np.asarray(x*0.1, 'd'),representation='wireframe') fig = mlab.gcf() ms = s.mlab_source for i in range(15): x, y = np.mgrid[0:3:1.0/(i+2),0:3:1.0/(i+2)] sc = np.asarray(x*x*0.05*(i+1), 'd') ms.reset(x=x, y=y, scalars=sc) fig.scene.reset_zoom() yield anim2() show()
上述就是小編為大家分享的怎么在python中使用mayavi實現(xiàn)三維繪圖了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:怎么在python中使用mayavi實現(xiàn)三維繪圖-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.ef60e0e.cn/article/cdosji.html