簡體   English   中英

Plot 非結構化三角形表面 Python

[英]Plot unstructured triangular surfaces Python

我正在嘗試 plot 使用 >10000 個非結構化三角形創建的表面。 我有三角形點的坐標和每個三角形點列表。 我的數據如下,

0.1 0.2 0.1
0.2 0.4 0.6
0.4 0.6 0.4
.
.
.
1 2 3
.
.
.

前三行是點的坐標(-X,Y,Z COORDINATES-)(第 1 行中的點 1,第 2 行中的點 2 等)。 點數超過 10000。“1 2 3”表示我們有一個三角形,其中的角點是 1、2 和 3。所以,我想通過從第一個三角形開始並繪制它們來 plot 表面逐個。 我嘗試按照上述程序進行操作,但我沒有得到正確的數字,最后我收到以下錯誤消息。

圖形尺寸 432x288 與 0 軸

我已經嘗試了以下代碼。

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
# from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

fileName = open('surface.txt','r')
print(fileName.readline())
dummy = fileName.readline().split()
npo = int(dummy[2])
nel = int(dummy[4])

xp = np.zeros([npo])
yp = np.zeros([npo])
zp = np.zeros([npo])


el1 = np.zeros([nel])
el2 = np.zeros([nel])
el3 = np.zeros([nel])

for i in range(0,npo):
    dummy = fileName.readline().split()
    xp[i] = float(dummy[0])
    yp[i] = float(dummy[1])
    zp[i] = float(dummy[2])
    # print(i,xp[i],yp[i],zp[i])
for i in range(0,nel):
    dummy = fileName.readline().split()
    el1[i] = int(dummy[0])
    el2[i] = int(dummy[1])
    el3[i] = int(dummy[2])


fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
for i in range(0,nel):
    x1 = xp[int(el1[i])-1]
    y1 = yp[int(el1[i])-1]
    z1 = zp[int(el1[i])-1]
    
    x2 = xp[int(el2[i])-1]
    y2 = yp[int(el2[i])-1]
    z2 = zp[int(el2[i])-1]

    x3 = xp[int(el3[i])-1]
    y3 = yp[int(el3[i])-1]
    z3 = zp[int(el3[i])-1]

    xarr = [x1,x2,x3,x1]
    yarr = [y1,y2,y3,y1]
    zarr = [z1,z2,z3,z1]
    
  
    verts = [list(zip(xarr,yarr,zarr))]
    ax2.add_collection3d(Poly3DCollection(verts))

ax2.set_xbound(0,1)
ax2.set_ybound(0,1)
ax2.set_zbound(0,3)

我會很高興聽到你的意見。

function plo_trisurf完全符合您的要求。

  • x, y, z是三角形的節點
  • tri包含三角形節點的索引

一個小例子:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.array([0, -1, 1, 1])
y = np.array([0, 1, -1, 1])
z = np.array([0, 1, 1, -1])

tri = np.array([[0, 1, 2],
                [0, 1, 3],
                [0, 2, 3]])

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_trisurf(x, y, z, triangles=tri)

trisurf 示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM