日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 3d绘图模块_使用python和mayavi创建3D streamplot

發布時間:2023/12/10 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 3d绘图模块_使用python和mayavi创建3D streamplot 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目前很容易使用

python和matplotlib生成2D streamplot,因為最近

streamplot由Tom Flannaghan和Tony Yu合并到matplotlib.

雖然可以使用matplotlib生成某些類型的3D繪圖,但目前不支持3D streamplots.然而,python繪圖程序mayavi(提供基于vtk的繪圖的python接口)能夠使用其flow()功能的3D streamplots.

我已經創建了一個簡單的python模塊來對2D和3D中的數據進行流繪圖,在3D數據集中沒有Z斜率(所有dZ = 0),以演示我在mayavi與matplotlib有效匹配數據時所面臨的繪圖挑戰在xy平面上.注釋的代碼和結果圖如下所示:

import numpy, matplotlib, mayavi, matplotlib.pyplot, mayavi.mlab

#for now, let's produce artificial streamline data sets for 2D & 3D cases where x and y change by +1 at each point, while Z changes by 0 at each point:

#2D data first:

x = numpy.ones((10,10))

y = numpy.ones((10,10))

#and a corresponding meshgrid:

Y,X = numpy.mgrid[-10:10:10j,-10:10:10j]

#now 3D data with Z = 0 (i.e., I want to be able to produce a matching streamplot plane in mayavi, which is normally used for 3D):

xx = numpy.ones((10,10,10))

yy = numpy.ones((10,10,10))

zz = numpy.zeros((10,10,10))

#and a corresponding meshgrid:

ZZ,YY,XX = numpy.mgrid[-10:10:10j,-10:10:10j,-10:10:10j]

#plot the 2D streamplot data with matplotlib:

fig = matplotlib.pyplot.figure()

ax = fig.add_subplot(111,aspect='equal')

speed = numpy.sqrt(x*x + y*y)

ax.streamplot(X, Y, x, y, color=x, linewidth=2, cmap=matplotlib.pyplot.cm.autumn,arrowsize=3)

fig.savefig('test_streamplot_2D.png',dpi=300)

#there's no streamplot 3D available in matplotlib, so try to see how mayavi behaves with a similar 3D data set:

fig = mayavi.mlab.figure(bgcolor=(1.0,1.0,1.0),size=(800,800),fgcolor=(0, 0, 0))

st = mayavi.mlab.flow(XX,YY,ZZ,xx,yy,zz,line_width=4,seedtype='sphere',integration_direction='forward') #sphere is the default seed type

mayavi.mlab.axes(extent = [-10.0,10.0,-10.0,10.0,-1.0,1.0]) #set plot bounds

fig.scene.z_plus_view() #adjust the view for a perspective along z (xy plane flat)

mayavi.mlab.savefig('test_streamplot_3D_attempt_1.png')

#now start to modify the mayavi code to see if I can 'seed in' more streamlines (default only produces a single short streamline, albeit of the correct slope)

#attempt 2 uses a line seed / widget over the specified bounds (points 1 and 2):

fig = mayavi.mlab.figure(bgcolor=(1.0,1.0,1.0),size=(800,800),fgcolor=(0, 0, 0))

st = mayavi.mlab.flow(XX,YY,ZZ,xx,yy,zz,line_width=4,seedtype='line',integration_direction='forward') #line instead of sphere

st.seed.widget.point1 = [0,-10,0]

st.seed.widget.point2 = [0,10,0] #so seed line should go up along y axis

st.seed.widget.resolution = 25 #seems to be the number of seeds points along the seed line

mayavi.mlab.axes(extent = [-10.0,10.0,-10.0,10.0,-1.0,1.0]) #set plot bounds

fig.scene.z_plus_view() #adjust the view for a perspective along z (xy plane flat)

mayavi.mlab.savefig('test_streamplot_3D_attempt_2.png')

#attempt 3 will try to seed a diagonal line across the plot to produce streamlines that cover the full plot:

#would need to use 'both' for integration_direction if I could get the diagonal seed line to work

fig = mayavi.mlab.figure(bgcolor=(1.0,1.0,1.0),size=(800,800),fgcolor=(0, 0, 0))

st = mayavi.mlab.flow(XX,YY,ZZ,xx,yy,zz,line_width=4,seedtype='line',integration_direction='forward')

st.seed.widget.point1 = [-10,10,0] #start seed line at top left corner of plot

st.seed.widget.point2 = [10,-10,0] #end seed line at bottom right corner of plot

#this fails to produce a diagonal seed line though

st.seed.widget.resolution = 25 #seems to be the number of seeds points along the seed line

mayavi.mlab.axes(extent = [-10.0,10.0,-10.0,10.0,-1.0,1.0]) #set plot bounds

fig.scene.z_plus_view() #adjust the view for a perspective along z (xy plane flat)

mayavi.mlab.savefig('test_streamplot_3D_attempt_3.png')

2D matplotlib結果(注意斜率1的流線與dx和dy數組完全匹配,所有dl和dy數組都填充了單位值):

3D mayavi結果(嘗試1;請注意,此處存在的單個流線的斜率是正確的,但流線的長度和數量顯然與2D matplotlib示例完全不同):

3D mayavi結果(嘗試2;注意我使用具有足夠高分辨率的線種子產生了更多適當斜率的流線,但也注意到代碼中指定的黑種子線的開始和結束x坐標不符合情節)

最后,嘗試#3(令人困惑)產生與#2完全相同的圖,盡管規定了不同的種子/小部件點.所以,問題是:我怎樣才能更好地控制種子線的位置以使其成為對角線?更廣泛地說,我希望能夠提供任意數量的種子點以獲得更一般的流圖3D(非平面)問題,但是用線來解決前一個特定情況應該讓我開始.

我在處理這個問題時發現了一些其他有用的資源,這些資源并沒有完全解決我的問題:

> matplotlib streamplot共同作者Tom Flannaghan關于這個主題的blog post

> example,其中自定義種子數組與mayavi Streamline的子類一起使用(反過來,由flow()使用),但遺憾的是實現細節不足以重現.

>使用mayavi生成2D streamplots of magnetic field lines的示例,包括注釋的源代碼.

> Similar examples with source in 3D,但仍不足以解決我的密謀問題:

> mayavi標準文檔中提供的flow()plot resulting from the example code(在流線后面可以看到球形種子窗口小部件).

總結

以上是生活随笔為你收集整理的python 3d绘图模块_使用python和mayavi创建3D streamplot的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。