對(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庫(kù)中的繪圖函數(shù)有很多候選參數(shù),但下文記錄并沒(méi)有過(guò)多討論,本人也是需要用到才查看手冊(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ù)的等高線(xiàn) 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圖比例的原因看起來(lái)并沒(méi)有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)之間繪制線(xià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差不多,只不過(guò)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í)行后并沒(méi)有動(dòng)畫(huà)效果,下面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來(lái)更改屬性值
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爬蟲(chóng)爬取金融期貨數(shù)據(jù)的案例分析
從技術(shù)角度來(lái)看,經(jīng)過(guò)一步步解析,任務(wù)是簡(jiǎn)單的,入門(mén)requests爬蟲(chóng)及入門(mén)pandas數(shù)據(jù)分析就可以完成,本文重點(diǎn)給大家介紹Python爬蟲(chóng)爬取金融期貨數(shù)據(jù)的案例分析,感興趣的朋友一起看看吧2022-06-06
fastapi與django異步的并發(fā)對(duì)比分析
這篇文章主要介紹了fastapi與django異步的并發(fā)對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Python對(duì)130w+張圖片檢索的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Python對(duì)130w+張圖片檢索的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python3中內(nèi)置類(lèi)型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換 問(wèn)題
這篇文章主要介紹了Python3中內(nèi)置類(lèi)型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Django框架實(shí)現(xiàn)在線(xiàn)考試系統(tǒng)的示例代碼
這篇文章主要介紹了Django框架實(shí)現(xiàn)在線(xiàn)考試系統(tǒng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python繪圖庫(kù)Pyecharts可視化效果示例詳解
本文將帶您從零開(kāi)始,逐步掌握使用Pyecharts庫(kù)進(jìn)行數(shù)據(jù)可視化的技能,Pyecharts是一個(gè)基于Echarts的Python可視化庫(kù),能夠輕松創(chuàng)建各種交互式圖表和地圖,無(wú)論您是數(shù)據(jù)分析新手還是有經(jīng)驗(yàn)的開(kāi)發(fā)者,都能幫助您深入了解Pyecharts的使用2023-08-08
python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實(shí)例解析
這篇文章主要介紹了python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

