最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

對python mayavi三維繪圖的實現(xiàn)詳解

 更新時間:2019年01月08日 11:22:22   作者:落葉_小唱  
今天小編就為大家分享一篇對python mayavi三維繪圖的實現(xiàn)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

網(wǎng)上下載mayavi的官方幫助文檔,里面有很多例子,下面的記錄都是查看手冊后得到的。

http://code.enthought.com/projects/mayavi/docs/development/latex/mayavi/mayavi_user_guide.pdf

python的mayavi.mlab庫中的繪圖函數(shù)有很多候選參數(shù),但下文記錄并沒有過多討論,本人也是需要用到才查看手冊的。

安裝好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)

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

python mayavi三維繪圖

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

python mayavi三維繪圖

 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三維繪圖

二、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三維繪圖

三、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三維繪圖

四、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三維繪圖

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

python mayavi三維繪圖

六、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三維繪圖

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,

python mayavi三維繪圖

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

python mayavi三維繪圖

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

python mayavi三維繪圖

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

python mayavi三維繪圖

九、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三維繪圖

十、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 #默認(rèn)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)詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python讀取YUV文件,并顯示的方法

    Python讀取YUV文件,并顯示的方法

    今天小編就為大家分享一篇Python讀取YUV文件,并顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析

    利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析

    從技術(shù)角度來看,經(jīng)過一步步解析,任務(wù)是簡單的,入門requests爬蟲及入門pandas數(shù)據(jù)分析就可以完成,本文重點給大家介紹Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析,感興趣的朋友一起看看吧
    2022-06-06
  • Python頁面加載的等待方式總結(jié)

    Python頁面加載的等待方式總結(jié)

    在本篇內(nèi)容里小編給大家整理的是關(guān)于Python頁面加載的等待方式總結(jié)內(nèi)容,有需要的朋友們可以參考下。
    2021-02-02
  • fastapi與django異步的并發(fā)對比分析

    fastapi與django異步的并發(fā)對比分析

    這篇文章主要介紹了fastapi與django異步的并發(fā)對比分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python對130w+張圖片檢索的實現(xiàn)方法

    Python對130w+張圖片檢索的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Python對130w+張圖片檢索的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換 問題

    Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換 問題

    這篇文章主要介紹了Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • Django框架實現(xiàn)在線考試系統(tǒng)的示例代碼

    Django框架實現(xiàn)在線考試系統(tǒng)的示例代碼

    這篇文章主要介紹了Django框架實現(xiàn)在線考試系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python PIL庫圖片灰化處理

    Python PIL庫圖片灰化處理

    這篇文章主要介紹了Python圖片灰化處理PIL庫的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Python繪圖庫Pyecharts可視化效果示例詳解

    Python繪圖庫Pyecharts可視化效果示例詳解

    本文將帶您從零開始,逐步掌握使用Pyecharts庫進(jìn)行數(shù)據(jù)可視化的技能,Pyecharts是一個基于Echarts的Python可視化庫,能夠輕松創(chuàng)建各種交互式圖表和地圖,無論您是數(shù)據(jù)分析新手還是有經(jīng)驗的開發(fā)者,都能幫助您深入了解Pyecharts的使用
    2023-08-08
  • python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實例解析

    python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實例解析

    這篇文章主要介紹了python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實例解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01

最新評論

通河县| 台中市| 绥宁县| 玛沁县| 黎城县| 承德县| 栾城县| 延边| 通州区| 临泉县| 浙江省| 锦州市| 云林县| 来宾市| 崇义县| 建湖县| 霸州市| 兰考县| 浙江省| 简阳市| 宜阳县| 泸定县| 瓦房店市| 宜兰市| 株洲县| 鹰潭市| 海南省| 山阳县| 五大连池市| 新乡市| 龙里县| 陇西县| 汕头市| 阳谷县| 昌都县| 芒康县| 玛沁县| 延安市| 隆尧县| 建宁县| 资溪县|