溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

發(fā)布時(shí)間:2021-04-20 17:39:49 來(lái)源:億速云 閱讀:497 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

Python主要用來(lái)做什么

Python主要應(yīng)用于:1、Web開(kāi)發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲(chóng);4、嵌入式應(yīng)用開(kāi)發(fā);5、游戲開(kāi)發(fā);6、桌面應(yīng)用開(kāi)發(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)

首先從幫助手冊(cè)上了解下mayavi的colormap,如下圖:

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

下面列舉常用的三維繪圖函數(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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

 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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

二、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

三、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

四、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

五、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

六、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

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,

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

七、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

八、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

參數(shù)的mode可選項(xiàng)如下圖:

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

九、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()

怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖

十、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()

上述就是小編為大家分享的怎么在python中使用mayavi實(shí)現(xiàn)三維繪圖了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI