對(duì)python mayavi三維繪圖的實(shí)現(xiàn)詳解
網(wǎng)上下載mayavi的官方幫助文檔,里面有很多例子,下面的記錄都是查看手冊(cè)后得到的。
http://code.enthought.com/projects/mayavi/docs/development/latex/mayavi/mayavi_user_guide.pdf
python的mayavi.mlab庫中的繪圖函數(shù)有很多候選參數(shù),但下文記錄并沒有過多討論,本人也是需要用到才查看手冊(cè)的。
安裝好mayavi2的繪圖環(huán)境后,可以結(jié)合numpy進(jìn)行科學(xué)繪圖,在代碼中事先加入如下代碼:
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)
首先從幫助手冊(cè)上了解下mayavi的colormap,如下圖:
下面列舉常用的三維繪圖函數(shù)和簡(jiǎn)單例子。
一、barchart
* barchart(s, ...)
* barchart(x, y, s, ...)
* barchart(x, y, f, ...)
* barchart(x, y, z, s, ...)
* barchart(x, y, z, f, ...)
如果只傳遞一個(gè)參數(shù),可以是一維(1-D),二維(2-D)或3維(3-D)的給定向量長(zhǎng)度的數(shù)組;
如果傳遞三個(gè)參數(shù)(x,y,s)或(x,y,f),x,y是對(duì)應(yīng)于數(shù)組s的二維(2-D)坐標(biāo),也可以是可調(diào)用的函數(shù)f,該函數(shù)返回?cái)?shù)組;
四個(gè)參數(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) #生成隨機(jī)的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)對(duì)應(yīng)下的值,下面繪制的是matlab的peaks函數(shù)三維圖,可能是因?yàn)槔L圖比例的原因看起來并沒有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就被認(rèn)為是數(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()
這里只傳遞了一個(gè)參數(shù)pk_z,
七、plot3d
* plot3d(x, y, z, ...)
* plot3d(x, y, z, s, ...)
數(shù)據(jù)點(diǎn)之間繪制線段,x,y,z,s都是具有相同shape的numpy數(shù)組或列表(list),x,y,z是三維坐標(biāo),也就是空間中數(shù)據(jù)點(diǎn)的位置
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)下的點(diǎn)(x,y,z),仍然用前面的例子。
t=mgrid[-pi:pi:50j] s=sin(t) # 參數(shù)s是設(shè)置每個(gè)點(diǎn)的大小(scalar),mode可選 mlab.points3d(cos(t),sin(3*t),cos(5*t),s,mode='sphere',line_width=1) mlab.colorbar() mlab.show()
參數(shù)的mode可選項(xiàng)如下圖:
九、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
繪制三維動(dòng)圖,幫助文檔上的代碼執(zhí)行后并沒有動(dòng)畫效果,下面2個(gè)示例代碼是查看了mayavi的相關(guān)源碼后總結(jié)的,大家也可以直接查看相關(guān)源碼查看更多官方提供的示例代碼。
(1)
@animate(delay=200) # 設(shè)置延時(shí)時(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 #默認(rèn)500ms延時(shí) 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()
以上這篇對(duì)python mayavi三維繪圖的實(shí)現(xiàn)詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Python的Flask框架中使用日期和時(shí)間的教程
這篇文章主要介紹了在Python的Flask框架中使用日期和時(shí)間的教程,包括對(duì)各個(gè)時(shí)區(qū)之間轉(zhuǎn)換的一些處理,需要的朋友可以參考下2015-04-04python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Python?pandas刪除指定行/列數(shù)據(jù)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python?pandas刪除指定行/列數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法,結(jié)合具體實(shí)例形式分析了Python針對(duì)json轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06解決python os.mkdir創(chuàng)建目錄失敗的問題
今天小編就為大家分享一篇解決python os.mkdir創(chuàng)建目錄失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10pytorch 準(zhǔn)備、訓(xùn)練和測(cè)試自己的圖片數(shù)據(jù)的方法
這篇文章主要介紹了pytorch 準(zhǔn)備、訓(xùn)練和測(cè)試自己的圖片數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01