gpt4 book ai didi

python - 绘制非结构化三角形曲面 Python

转载 作者:行者123 更新时间:2023-12-04 04:02:49 25 4
gpt4 key购买 nike

我正在尝试绘制一个由 >10000 个非结构化三角形创建的表面。我有三角形点的坐标和每个三角形点列表。我的数据如下,

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

前三行是点(第 1 行中的点 1,第 2 行中的点 2 等)的坐标(-X、Y、Z 坐标-)。点数超过10000。“1 2 3”表示我们有一个三角形,其角点为 1、2 和 3。所以,我想通过从第一个三角形开始并一个一个地绘制它们来绘制曲面。我已尝试按照上述步骤操作,但我没有得到正确的数字,最后我收到以下错误消息。

Figure size 432x288 with 0 Axes

我试过下面的代码。

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)

很高兴听到您的意见。

最佳答案

函数 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)

Example of trisurf

关于python - 绘制非结构化三角形曲面 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62828893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com